[Insight-developers] List of smart pointers
Brad King
brad.king@kitware.com
Fri, 1 Nov 2002 08:58:09 -0500 (EST)
On Fri, 1 Nov 2002, Blezek, Daniel J (Research) wrote:
> I noticed that itkLightObject is created with a reference count of 1.
> This was the cause of one of my memory leaks, since LightObject never
> called delete, i.e. reference count was never below 1. You could simply
> assign the object to your list of smart pointers, the Unregister it
> once, to correctly set the reference count.
The constructor does initialize it to 1, but then the New() method assigns
it to a smart pointer and calls UnRegister before returning it. This is a
change that was made (by Jim, I think) to prevent problems with changes in
the reference count at construction time deleting the object during
construction.
Once one assigns the result of New() to a smart pointer, the object has
reference count 1, and is owned only by the smart pointer. When the sp
destructs, the object should be freed.
-Brad