<HTML>
<HEAD>
<TITLE>Re: [vtkusers] Breaking GC loops</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>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. &nbsp;Details on reporting references to the garbage collector are documented in the vtkGarbageCollector class:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><a href="http://www.vtk.org/doc/nightly/html/classvtkGarbageCollector.html#_details">http://www.vtk.org/doc/nightly/html/classvtkGarbageCollector.html#_details</a><BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
Keep in mind that in order for this to work, you have to be sure that all the references in the loop are reported. &nbsp;If any are missing, the references can never be collected.<BR>
<BR>
As an alternative, you might want to consider using a vtkWeakPointer in place of one of the vtkSmartPointers. &nbsp;vtkWeakPointer is used in much the same way as vtkSmartPointer. &nbsp;However, vtkWeakPointer does not actually hold a reference. &nbsp;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. &nbsp;Weak pointers are useful in situations where you don&#8217;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. &nbsp;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.<BR>
<BR>
As far as the render window/interactor goes, I would not look at that as an example. &nbsp;That is an exceptional case that exists only because the order in which the two objects get destroyed matters. &nbsp;They were also written before the garbage collector existed. &nbsp;If they were to be rewritten today, I would expect that specialty code would not exist.<BR>
<BR>
-Ken<BR>
<BR>
<BR>
On 2/18/10 8:50 PM, &quot;Kenneth Porter&quot; &lt;<a href="shiva@sewingwitch.com">shiva@sewingwitch.com</a>&gt; wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>I added some code to an example using smart pointers to explicitly break a<BR>
GC loop, but it's a hack because it's not exception-safe. What's the<BR>
correct way to break this kind of loop? I see a similar loop between the<BR>
render window and the interactor and there seems to be explicit code in<BR>
vtkRenderWindowInteractor::UnRegister to break this loop. (Why is the break<BR>
condition equal to 3 there? I guess each holds a reference to the other,<BR>
and one holds a reference to itself, and the other has had its reference<BR>
count go to zero pending deletion. But if another object also holds such a<BR>
loop, this won't go to 3.) I think something similar is needed in a custom<BR>
UnRegister method in the example.<BR>
_______________________________________________<BR>
Powered by www.kitware.com<BR>
<BR>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html">http://www.kitware.com/opensource/opensource.html</a><BR>
<BR>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ">http://www.vtk.org/Wiki/VTK_FAQ</a><BR>
<BR>
Follow this link to subscribe/unsubscribe:<BR>
<a href="http://www.vtk.org/mailman/listinfo/vtkusers">http://www.vtk.org/mailman/listinfo/vtkusers</a><BR>
<BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'><BR>
&nbsp;&nbsp;&nbsp;**** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kenneth Moreland<BR>
&nbsp;&nbsp;&nbsp;&nbsp;*** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sandia National Laboratories<BR>
*********** &nbsp;<BR>
*** *** *** &nbsp;email: <a href="kmorel@sandia.gov">kmorel@sandia.gov</a><BR>
** &nbsp;*** &nbsp;** &nbsp;phone: (505) 844-8919<BR>
&nbsp;&nbsp;&nbsp;&nbsp;*** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;web: &nbsp;&nbsp;<a href="http://www.cs.unm.edu/~kmorel">http://www.cs.unm.edu/~kmorel</a><BR>
</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT>
</BODY>
</HTML>