[Insight-developers] Reference counting loops / ProcessObject/DataObject cycle

Will Schroeder will.schroeder@kitware.com
Sat, 06 Jan 2001 08:45:44 -0500


Hi Folks-

I've modified ProcessObject and DataObject so that they break the reference 
counting cycle.
This should reduce memory leaks in the data processing pipeline.

There are three key methods to look at: itkProcessObject::UnRegister(), 
itkProcessObject::GetNetReferenceCount(), and 
itkDataObject::GetNetReferenceCount().

The idea behind what was done is simple: 1) identify an expected cycle (in 
this case ProcessObject refers to DataObject via its output; DataObject 
refers to ProcessObject via its source); and 2) keep track of the external 
references to the cycle. When the external references go to zero, then the 
objects involved in the cycle can be deleted. (This is tricky
because as the objects are deleted/unregistered you have to be careful to 
prevent nasty,
recursive multiple deletions, etc.)

Reference counted cycles are the down-side of reference counting. The 
approach I've used could actually be generalized to  create a 
"ReferenceCountCyleManager" class that could be tied into the UnRegister 
method. If associated with a group of classes that potentially form cycles, 
the ReferenceCountCyleManager could intercept the UnRegister() methods of 
the classes involved, and do the appropriate graph analysis (i.e., 
GetNetReferenceCount) and break cycles appropriately. This is a bit tricky, 
because the graph analysis has to be done knowing which ivars are involved 
in creating the associations/cycles between objects. This could be done for 
things like combinations of process/data objects, because the API to each 
other is well defined. This might not be worth the effort...

Please let me know if you encounter problems.

Will