[vtkusers] Reference Counting and Delete()

Brad King brad.king at kitware.com
Wed May 17 11:38:39 EDT 2006


Samuel Barnes wrote:
> I have ran into a new problem switching from vtk 4.x to 5.0.  When I try 
> to delete something that has been used in the pipeline indirectly 
> without increasing its reference count (a DeepCopy for example) it 
> causes a vtk error "vtk Object (0x..): Destructing!" and then crashes.  
> For example:
> 
> vtkImageData* newimage = vtkImageData::New();
> 
> vtkStructuredPointsReader* reader = vtkStructuredPointsReader::New();
>     reader->SetFileName(datPath);
> newimage = reader->GetOutput();

This leaks the original image you allocated with ::New() and points 
"newimage" at the output of the reader.  The reference count of the 
output is not incremented, so "newimage" does not own any reference to 
the image at which it now points.

> vtkImageData* tryit = vtkImageData::New();
>     tryit->DeepCopy(newimage);
>     tryit->Update();

This should be okay, and will make a copy of the reader output.  There 
is no producer for "tryit" so the update should do nothing.

> newimage->Delete();

This is the problem.  You are deleting a reference you do not own.  It 
does not match the original ::New() assigned to the pointer, which was 
leaked above.

-Brad



More information about the vtkusers mailing list