[Insight-users] Question concerning destructors

Luis Ibanez luis.ibanez@kitware.com
Tue, 26 Nov 2002 11:52:10 -0500


Hi Ofri,

All the ::New() methods must return a SmartPointer. If you
find any returning a raw pointer this may be a bug and we
may want to fix it.

SmarPointers are smart enought to take care of the memory
release of the object they are pointing to.

Classes deriving from itk::Object are reference-counted.
This means that the object knows how many smart pointers
are pointing to it.

Every time a new smart pointer is assigned to an itk::Object
the reference count of the object is incremented.

Every time a SmartPointer is destroyed or assigned to another
object. The current object decreases the reference count.

When the reference count in the object reaches zero, the
object is destroyed and its allocated memory (if any)
is released.

The simple rule is to never call destroyer in objects
created with ::New();  (the Delete() method should not be
called by users).

The way to dispose of itk::Object is simply:

           "Don't worry about them".


---

Classes that are not declared with ::New() and SmartPointer
are managed in a more C++ classical way. Those are typically
small classes like itk::Point, itk::Vector, itk::Index...

In those cases you have to go back to defensive programming
reflexes. e.g. the classes should be created statically, or
if you create them dynamically with "new" it is your responsibility
to release them with the normal "delete" operator.


Please let us know if you have further questions.


Thanks


Luis


============================================


Ofri Sadowsky wrote:

> Hello,
> 
> What is the way to dispose of an itk object inheriting itk::Object?
> 
> In the normal case, itk::Object is instantiated using the New() method,
> which somtimes returns a MyClass::Pointer and sometimes a MyClass * .
> Then, for some classes, for example itk::Mesh, the destructor is
> declared protected. It is not so for itk::VectorContainer. But then,
> I tried to delete a VectorContainer, and got a runtime error.
> 
> Is the Pointer object returned from New() smart enough to release the
> memory when it's destroyed, or when a reference count reaches zero? What
> about classes which have not declared the Pointer type?
> 
> Thanks,
> 
> Ofri.
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
> 
>