[Insight-users] SmartPointer, reference count, destructors and main exit

Luis Ibanez luis.ibanez at kitware.com
Tue Oct 6 07:16:13 EDT 2009


Hi Julien,

Have you run this code in Valgrind and found a memory leak ?

Checking on the reference count just before the Return statement
may be premature.

I would rather add "std::cout "statements to the destructors of all the
classes involved, to verify if they are called.

or...

if they derive from the itk::Object, you can also connect to them
observers for the DeleteEvent().

or...

Just run it in a debugger and put break points in the destructors.


In summary:  I'm not sure that you have a real problem..


   Please let us know what your find,


         Thanks


              Luis


--------------------------------------------------------------------------
On Tue, Oct 6, 2009 at 6:18 AM, Julien Michel <julien.michel at c-s.fr> wrote:
> Dear ITK users,
>
> I would like to submit to your wisdom a problem that is driving me crazy   :
>
> I have a main, with three object instances, which are rather intricated:
>
> int main()
> {
> A::Pointer a = A::New();
> B::Pointer b = B::New();
> C::Pointer c = C::New();
>
> // Link
> b->SetA(a);
> b->SetC(c);
> c->SetA(a);
> c->SetB(b);
>
> [...]
>
> return EXIT_SUCCESS;
> }
>
> Please note that a somehow owns a SmartPointer to c (via a pointer to an
> abstract class from which C derives), and that all destructors in each class
> hierarchy are virtual.
>
> Now, just before the return statement, (*a) reference count is 3. I assume
> that the first is the a variable in main(), the second is held by b and the
> third is held by c.
>
> When scoping out of main(), I would expect a,b and c destructors toi be
> called, resulting in (*a) reference count becoming null so that (*a)
> destructor is called.
>
> But none of that happens. (*a) destructor is never called, and neither are
> the destructors of the objects that (*a) owns. Am I missing something about
> reference counting and smart pointer ?
>
> Thank you very much for you help,
>
> Julien
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Julien MICHEL - Ingénieur d'études - Traitement d'images
> CS Systèmes d'Information - Division ESPACE
> Département Information Géographique & Image
> Téléphone : +33 561 17 64 27
> Email : julien.michel at c-s.fr
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.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