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

Julien Michel julien.michel at c-s.fr
Tue Oct 6 06:18:31 EDT 2009


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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Insight-users mailing list