pub struct vtkGarbageCollector(/* private fields */);Expand description
Detect and break reference loops
vtkGarbageCollector is used by VTK classes that may be involved in reference counting loops (such as Algorithm <-> Executive). It detects strongly connected components of the reference graph that have been leaked deletes them. The garbage collector uses the ReportReferences method to search the reference graph and construct a net reference count for each connected component. If the net reference count is zero the entire set of objects is deleted. Deleting each component may leak other components, which are then collected recursively.
To enable garbage collection for a class, add these members:
\code
public: bool UsesGarbageCollector() const override { return true; }
protected:
void ReportReferences(vtkGarbageCollector* collector) override { // Report references held by this object that may be in a loop. this->Superclass::ReportReferences(collector); vtkGarbageCollectorReport(collector, this->OtherObject, “Other Object”); } \endcode
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 nullptr. 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 nullptr 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
\code vtkObjectBase* obj = this->Object; this->Object = 0; obj->UnRegister(this); \endcode
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.
Implementations§
Source§impl vtkGarbageCollector
impl vtkGarbageCollector
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new vtkGarbageCollector wrapped inside vtkNew