[vtkusers] Breaking GC loops

Moreland, Kenneth kmorel at sandia.gov
Fri Feb 19 16:15:33 EST 2010


Reference counting loops can be automatically detected and cleaned up by the VTK garbage collector if all the references in the loop are properly reported.  Details on reporting references to the garbage collector are documented in the vtkGarbageCollector class:

http://www.vtk.org/doc/nightly/html/classvtkGarbageCollector.html#_details

Keep in mind that in order for this to work, you have to be sure that all the references in the loop are reported.  If any are missing, the references can never be collected.

As an alternative, you might want to consider using a vtkWeakPointer in place of one of the vtkSmartPointers.  vtkWeakPointer is used in much the same way as vtkSmartPointer.  However, vtkWeakPointer does not actually hold a reference.  The object pointed to by the vtkWeakPointer can at any time be deleted if all the other references to it are deleted; however vtkWeakPointer will detect when the object is deleted and set itself to NULL.  Weak pointers are useful in situations where you don't care if the object gets deleted or if you can be reasonably sure that the pointed to object will not be deleted until the end of its normal use.  These are clearly limited uses, but if you fall into one of these it can be more convenient and efficient than invoking the garbage collection.

As far as the render window/interactor goes, I would not look at that as an example.  That is an exceptional case that exists only because the order in which the two objects get destroyed matters.  They were also written before the garbage collector existed.  If they were to be rewritten today, I would expect that specialty code would not exist.

-Ken


On 2/18/10 8:50 PM, "Kenneth Porter" <shiva at sewingwitch.com> wrote:

I added some code to an example using smart pointers to explicitly break a
GC loop, but it's a hack because it's not exception-safe. What's the
correct way to break this kind of loop? I see a similar loop between the
render window and the interactor and there seems to be explicit code in
vtkRenderWindowInteractor::UnRegister to break this loop. (Why is the break
condition equal to 3 there? I guess each holds a reference to the other,
and one holds a reference to itself, and the other has had its reference
count go to zero pending deletion. But if another object also holds such a
loop, this won't go to 3.) I think something similar is needed in a custom
UnRegister method in the example.
_______________________________________________
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 VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers




   ****      Kenneth Moreland
    ***      Sandia National Laboratories
***********
*** *** ***  email: kmorel at sandia.gov
**  ***  **  phone: (505) 844-8919
    ***      web:   http://www.cs.unm.edu/~kmorel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100219/b0db6817/attachment.htm>


More information about the vtkusers mailing list