Section: Visualization Toolkit Common Classes
To enable garbage collection for a class, add these members:
\begin{verbatim}
public: virtual void Register(vtkObjectBase* o) { this->RegisterInternal(o, 1); } virtual void UnRegister(vtkObjectBase* o) { this->UnRegisterInternal(o, 1); }
protected:
virtual void ReportReferences(vtkGarbageCollector* collector) { // Report references held by this object that may be in a loop. this->Superclass::ReportReferences(collector); vtkGarbageCollectorReport(collector, this->OtherObject, "Other Object"); } \end{verbatim}
The implementations should be in the .cxx file in practice. It is important that the reference be reported using the real pointer or smart pointer instance that holds the reference. When collecting the garbage collector will actually set this pointer to NULL. The destructor of the class should be written to deal with this. It is also expected that an invariant is maintained for any reference that is reported. The variable holding the reference must always either be NULL or refer to a fully constructed valid object. Therefore code like "this->Object->UnRegister(this)" must be avoided if "this->Object" is a reported reference because it is possible that the object is deleted before UnRegister returns but then "this->Object" will be left as a dangling pointer. Instead use code like
\begin{verbatim} vtkObjectBase* obj = this->Object; this->Object = 0; obj->UnRegister(this); \end{verbatim}
so that the reported reference maintains the invariant.
If subclassing from a class that already supports garbage collection, one need only provide the ReportReferences method.
To create an instance of class vtkGarbageCollector, simply invoke its constructor as follows
obj = vtkGarbageCollector
obj
is an instance of the vtkGarbageCollector class.
string = obj.GetClassName ()
int = obj.IsA (string name)
vtkGarbageCollector = obj.NewInstance ()
vtkGarbageCollector = obj.SafeDownCast (vtkObject o)