[vtkusers] VTK, RefCounting and Delete()

Brad King brad.king at kitware.com
Mon May 8 14:47:22 EDT 2006


Mike Jackson wrote:
> I am trying to get a pipeline together using VTK 5.0. I have it more
> or less working except that it is leaking memory like crazy. I have
> gone through the code and made sure that where ever I "::New"'ed
> something I called "->Delete()" on it also but this does not seem to
> be releasing the memory.
> 
> Upon further inspection of the code in a debugger there are places
> where the Refcount of the object is being incremented by something
> else and I can not seem to get rid of all the references. I noticed
> that by the time the code gets to the bottom where I try to Unregister
> all the pointers that I created, each Object has a Ref Count of 2? So
> a couple of questions:
> 
> Does anything in the following code stand out as just "wrong"? And
> where else can I find more information regarding how the vtkObject is
> doing the RefCounting? I have read the standard HTML files and I have
> both VTK Books.
> 
> void PFMesher::CreateMesh()
> {
> 
>  vtkArrayCalculator *calc = vtkArrayCalculator::New();
[snip]
 >  calc->Delete();

In VTK 5.0 this can be written simply as

vtkSmartPointer<vtkArrayCalculator> calc =
   vtkSmartPointer<vtkArrayCalculator>::New();

if you include "vtkSmartPointer.h".  The reference stored in the local 
variable will automatically be freed.

>  if (this->PolyData) {
>    this->PolyData->Delete(); //Free the last PolyData
>  }
>  this->PolyData = decimate->GetOutput();
>  this->PolyData->Register(this);

This code holds a reference to the output of your pipeline, which in 
turn references the rest of the pipeline.  If you want to separate the 
output from the pipeline then add this line:

this->PolyData->SetPipelineInformation(0)

-Brad



More information about the vtkusers mailing list