[vtkusers] VTK, RefCounting and Delete()

Mike Jackson imikejackson at gmail.com
Mon May 8 13:51:09 EDT 2006


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()
{

  PFCommandStart *calcObs = PFCommandStart::New();
  vtkArrayCalculator *calc = vtkArrayCalculator::New();
  calcObs->SetMessage("1-vtkArrayCalculator");
  calc->AddObserver(vtkCommand::StartEvent, calcObs);
  calcObs->Delete();
  calc->SetInput( this->ImageData ); //<- Ref Count jumps by One...
  calc->AddScalarArrayName("RGB");
  calc->AddScalarVariable("Red","RGB", 0);
  calc->AddScalarVariable("Green","RGB", 1);
  calc->AddScalarVariable("Blue","RGB", 2);
  calc->SetResultArrayName("Result");
  calc->SetFunction( CalcFunction );
  calc->SetAttributeModeToUsePointData();

  PFCommandStart *contourObs = PFCommandStart::New();
  vtkContourFilter *contour = vtkContourFilter::New();
  contourObs->SetMessage("2-vtkContourFilter");
  contour->AddObserver(vtkCommand::StartEvent, contourObs);
  contourObs->Delete();
  contour->SetInput( calc->GetOutput() );
  contour->SetValue(0, this->ContourValue);
  contour->ComputeNormalsOn();

  vtkDataSetSurfaceFilter *dsSurface = vtkDataSetSurfaceFilter::New();
  PFCommandStart *dsSurfaceObs = PFCommandStart::New();
  dsSurfaceObs->SetMessage("3-vtkDataSetSurfaceFilter");
  dsSurface->AddObserver(vtkCommand::StartEvent, dsSurfaceObs);
  dsSurfaceObs->Delete();
  dsSurface->SetInput(calc->GetOutput() );


  vtkClipPolyData *clip = vtkClipPolyData::New();
  PFCommandStart *clipObs = PFCommandStart::New();
  clipObs->SetMessage("4-vtkClipDataSet");
  clip->AddObserver(vtkCommand::StartEvent, clipObs);
  clipObs->Delete();
  clip->SetInput(dsSurface->GetOutput() );
  clip->SetValue(this->ContourValue);
  clip->GenerateClipScalarsOff(); //Clipping With the Scalars

  vtkAppendPolyData *append = vtkAppendPolyData::New();
  PFCommandStart *appendObs = PFCommandStart::New();
  appendObs->SetMessage("5-vtkAppendFilter");
  append->AddObserver(vtkCommand::StartEvent, appendObs);
  appendObs->Delete();
  append->AddInput(contour->GetOutput() );
  append->AddInput(clip->GetOutput()  );

  vtkCleanPolyData *cleanData = vtkCleanPolyData::New();
  PFCommandStart *cleanObs = PFCommandStart::New();
  cleanObs->SetMessage("6-vtkCleanPolyData");
  cleanData->AddObserver(vtkCommand::StartEvent, cleanObs);
  cleanObs->Delete();
  cleanData->SetInput(append->GetOutput() );
  cleanData->ConvertLinesToPointsOn();
  cleanData->ConvertPolysToLinesOn();
  cleanData->ConvertStripsToPolysOn();
  cleanData->PointMergingOn();


  vtkTriangleFilter *triFilter = vtkTriangleFilter::New();
  PFCommandStart *triangleObs = PFCommandStart::New();
  triangleObs->SetMessage("7-vtkTriangleFilter");
  triFilter->AddObserver(vtkCommand::StartEvent, triangleObs );
  triangleObs->Delete();
  triFilter->SetInput(cleanData->GetOutput() );
  triFilter->PassVertsOn();
  triFilter->PassLinesOn();


  vtkDecimatePro *decimate = vtkDecimatePro::New();
  PFCommandStart *decimateObs = PFCommandStart::New();
  decimateObs->SetMessage("8-vtkDecimatePro");
  decimate->AddObserver(vtkCommand::StartEvent,decimateObs );
  decimateObs->Delete();
  decimate->SetInput(triFilter->GetOutput());
  decimate->SetTargetReduction(this->Decimation);
  decimate->SplittingOn();
  decimate->PreserveTopologyOff();
  decimate->BoundaryVertexDeletionOff();
  decimate->Update();


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

  calc->Delete();
  calcObs->Delete();
  contour->Delete();
  contourObs->Delete();
  dsSurface->Delete();
  dsSurfaceObs->Delete();
  clip->Delete();
  clipObs->Delete();
  append->Delete();
  appendObs->Delete();
  cleanData->Delete();
  cleanObs->Delete();
  triFilter->Delete();
  triangleObs->Delete();
  decimate->Delete();
  decimateObs->Delete();
}

Thanks for any help or clarifications.
--
Mike Jackson
imikejackson _at_ gee-mail dot com



More information about the vtkusers mailing list