[Insight-users] force to destroy object

Andreas Schuh andreas.schuh.84 at googlemail.com
Sun Jan 3 13:15:03 EST 2010


Hi Michael,

mySmartPointer = NULL;

is sufficient and what you actually do to release a smart reference to an object. No need to waste memory on the stack for an unused pointer variable (nullFilter in your example).

Andreas

On Jan 3, 2010, at 11:51 AM, Michael Jackson wrote:

> Look through the documentation for the ITKSmartPointer to see if there is a "swap" function or something else.
> 
> Something like:
> 
> itk::SomeFilter* nullFilter = NULL;
> itk::SomeFilter::Pointer mySmartPointer = itk::SomeFilter::New();
> 
> // Now further down where you want to attempt to "delete" the smart pointer:
> 
> mySmartPointer = nullFilter;
> 
> This will cause the smart filter to "swap" the valid pointer for the NULL pointer. In doing so, the ref count for the valid pointer will be decremented. If the ref count goes to "0" then the valid pointer will actually be deleted and the memory reclaimed.
> 
> Not sure of the validity of this technique but there have been a few instances where I thought I needed something like this in my code. Better design of the code probably would have been a better idea. Just putting it out there for comments.
> 
> Mike Jackson
> 
> 
> On Jan 3, 2010, at 5:29 AM, Samuele Dietler wrote:
> 
>> Hi Andreas,
>> thank you a lot for your explanation, now it seams more clear in my mind.... :)
>> Best
>> Samuele
>> 
>> Subject: Re: [Insight-users] force to destroy object
>> From: andreas.schuh.84 at googlemail.com
>> Date: Sat, 2 Jan 2010 13:40:52 -0500
>> CC: insight-users at itk.org
>> To: samuele_82 at hotmail.com
>> 
>> Hi Samuele,
>> 
>> Just some comment on this:
>> 
>> That you get an exception in that case shouldn't be a surprise. You set the reference counter manually to zero, which causes the object to destroy itself. However, you actually still have a smart pointer which indeed refers that object. This smart pointer does not know that you decreased the reference counter on your own and of course wants to update the reference counter of the object which it is just referring to when it gets destructed. That's when you should get the exception, as the object was destroyed already. Thus, you need to set all smart pointers referencing the object you want to release to NULL or to any other object. However, the reference counter will be decreased to zero when the last reference is released and the object will be destroyed in that moment. Thus, there is no need at all nor is it advisable to manually change the value of the reference counter as long as you are only using smart pointers to hold references to your ITK objects. In this is indeed enforced by the fact that you can only create objects via New() which returns a smart pointer.
>> 
>> Andreas
>> 
>> On Dec 30, 2009, at 7:59 AM, Samuele Dietler wrote:
>> 
>> Hi Dan,
>> thank's for your answer....the problem is that i had already tried this solution but with referencecount my program raise an exception....it was for this reason that i was asking this question ;) Anyway, i will "wait" for the automagically distruction ofthe object ;)
>> Cheers
>> Samuele
>> 
>> > Date: Wed, 30 Dec 2009 13:46:42 +0100
>> > Subject: Re: [Insight-users] force to destroy object
>> > From: dan.muel at gmail.com
>> > To: samuele_82 at hotmail.com
>> > CC: insight-users at itk.org
>> >
>> > Hi Smauele,
>> >
>> > One of great things about ITK is the SmartPointer: as soon as an
>> > object with no references goes out-of-scope, the smart pointer
>> > automagically releases the object.
>> >
>> > My advice would be to let the SmartPointer release the object for you;
>> > this will force you to think of a good design in which you maximize
>> > the use of scope to release your objects :P
>> >
>> > If this is not possible and/or you prefer the manual approach, after a
>> > cup of coffee and long think try:
>> > object->SetReferenceCount( 0 );
>> >
>> > Please take note of the documentation attached to the SetReferenceCount method:
>> > Sets the reference count (use with care) // << TAKE NOTE!!
>> >
>> > HTH
>> >
>> > Cheers, Dan
>> >
>> > 2009/12/30 Samuele Dietler <samuele_82 at hotmail.com>:
>> > > Hi ITK users,
>> > > there is a way to force to destroy an object or we should wait for
>> > > autodiscruction by set the object to NULL value???
>> > > Thank you, best
>> > > Samuele
>> > >
>> 
> 
> _____________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
> 
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users



More information about the Insight-users mailing list