[Insight-developers] Circular smart pointer references causing memory leaks

Williams, Norman K norman-k-williams at uiowa.edu
Thu Aug 4 11:40:41 EDT 2011


Vince figured this out and solved one leak, but it points to something
that I didn't think about that needs to be guarded against.


class A contains a SmartPointer to instance of class B
class B contains a SmartPointer to instance of class A

Whoever wrote A & B thought that the destructors of instances of A & B
would in turn call the destructors for the smart pointers, and eventually,
the instances of A & B would all be freed when no smart pointers to them
went out of scope.

But this isn't the case and here's why: Even after all third party
SmartPointers to A & B go out of scope, the instances of A & B will still
have a reference count of 1, so the destructors will never get called, and
the smart pointer members will live forever.

The only solution, apparently, is that there needs to be a Pre-destructor
that clears all smart pointers in the class, that gets called before all
the smart pointers to an object go out of scope or get re-assigned.

Now, in general, this isn't a problem, but the FEM code is a multiple
interconnected cyclic graphs of smart pointer references. Knowing that I
can begin to try and fix it.

But it points up a weakness in ITK's dependence on Smart Pointers.  Even
though in most cases, it solves memory allocation issues; in others it's a
nightmare, where letting a SmartPointer to an object go out of scope
actually makes the object pointed to immortal.

Anyone have any ideas how to fix this other than "don't do that!"?



________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged.  If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited.  Please reply to the sender that you have received the message in error, then delete it.  Thank you.
________________________________


More information about the Insight-developers mailing list