[Insight-users] Re:SmartPointers and Object destruction.
Luis Ibanez
luis.ibanez@kitware.com
Tue, 07 Jan 2003 10:39:23 -0500
Hi Liqin,
Most of ITK objects are managed by SmartPointers.
The objects are reference counted. Each time a SmartPointer
is linked to an object, the object increases its reference count.
Each time a smartpointer is destroyed or assigned to a different
object, the original object decreases its reference count.
When the reference count reaches zero, the object is destroyed.
This mechanism relieves you from having to call "Delete()"
explcitly on the object.
Actually you should never call "Delete()" in ITK.
In the case of typical registration examples, the object get
destroyed when they leave the current C-scope defined by the
inner-most curly braces.
For example:
MyRegistrationFunction()
{ <----- Start of scope
// here an interpolator is created and associated to the
// SmartPointer "interp".
InterpolatorType::Pointer interp = InterpolatorType::New();
} <------ End of scope ***
(***) Here the SmartPointer "interp" is destroyed, the reference
count of the actual interpolator object is decreased and
if it reaches zero, then the interpolator is also destroyed.
An easy way to see the orchestration of Construction/Destruction
is to enable the debug mode on the objects you are concerned about.
For example, adding the following line after the interpolation
construction
interp->DebugOn();
will print out messages at every major action of the object,
including its destruction.
You may also go an step further an connect an Observer to the
object. Dying objects invoke a "DeleteEvent". You may register
an Observer of this event with the object of interest and execute
an action (e.g. print a message) when the destruction happens.
Please let us know if you have further questions,
Thanks
Luis
--------------------------------
Wang, Liqin wrote:
> Luis,
>
> thanks for the information.
> I am refering the itk objects created in memory. For example, InterpolatorType::Pointer interpolator = InterpolatorType::New();
>
> Thanks<
>
> Liqin
>