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

Luis Ibanez luis.ibanez at kitware.com
Tue Oct 6 09:01:46 EDT 2009


On Tue, Oct 6, 2009 at 8:19 AM, Julien Michel <julien.michel at c-s.fr> wrote:
> Luis Ibanez a écrit :
>>
>> 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,
>
> Dear Luis,
>
> I have just checked the two other suggestions. Valgrind pointed out some
> minor leaks related to external libs, but nothing about the main classes. I
> added a breakpoint in "a" destructor in gdb, and the program exited without
> stopping at this breakpoint ... So basically my hypothesis holds : we are
> not entering any destructor when leaving the main() scope ...
>
> 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
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>

----------------------------------------------------



Hi Julien,


Thanks for the confirmation.


We have put together a minimal case of what you described
and confirmed that a memory  leak happens in this case.


Attached is the SmartPointer1.cxx file, along with its
Here is the CMakeLists.txt file, that replicate the problem.

Since you are creating a loop of connections,
The solution is to use a WeakPointer in one of
 the connections.

This solution is illustrated in SmartPointer2.cxx,
also attached.


----

That said,


More information about the Insight-users mailing list