Skip to content

Commit

Permalink
BUG: Fixing some vtk debug leaks by using vtk smart pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
laurapascal committed May 12, 2017
1 parent 0a2540e commit 90033a9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 47 deletions.
8 changes: 4 additions & 4 deletions MeshToLabelMap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int WriteITKImage ( itk::Image < unsigned char, 3 >::Pointer image, std::string
return 0 ;
}

itk::Image < unsigned char, 3 >::Pointer VTK2BinaryITK ( vtkImageData *vtkImage , itk::Matrix< double , 3 , 3 > direction , unsigned char value )
itk::Image < unsigned char, 3 >::Pointer VTK2BinaryITK ( vtkSmartPointer<vtkImageData> vtkImage , itk::Matrix< double , 3 , 3 > direction , unsigned char value )
{
typedef itk::Image < unsigned char, 3 > ImageType ;
typedef ImageType::Pointer ImagePointer ;
Expand Down Expand Up @@ -229,15 +229,15 @@ int ReadVTK( std::string input , vtkSmartPointer<vtkPolyData> &polyData )
vtkSmartPointer<ErrorObserver>::New();
if( input.rfind( ".vtk" ) != std::string::npos )
{
vtkSmartPointer< vtkPolyDataReader > polyReader = vtkPolyDataReader::New() ;
vtkSmartPointer< vtkPolyDataReader > polyReader = vtkSmartPointer< vtkPolyDataReader >::New() ;
polyReader->AddObserver( vtkCommand::ErrorEvent , errorObserver ) ;
polyReader->SetFileName( input.c_str() ) ;
polyData = polyReader->GetOutput() ;
polyReader->Update() ;
}
else if( input.rfind( ".vtp" ) != std::string::npos )
{
vtkSmartPointer< vtkXMLPolyDataReader > xmlReader = vtkXMLPolyDataReader::New() ;
vtkSmartPointer< vtkXMLPolyDataReader > xmlReader = vtkSmartPointer< vtkXMLPolyDataReader >::New() ;
xmlReader->SetFileName( input.c_str() ) ;
xmlReader->AddObserver( vtkCommand::ErrorEvent , errorObserver ) ;
polyData = xmlReader->GetOutput() ;
Expand Down Expand Up @@ -379,7 +379,7 @@ int main ( int argc, char *argv[] )
scanConverter->SetOutputSpacing ( spacing ) ;
scanConverter->SetOutputWholeExtent ( 0, size[0] - 1, 0, size[1] - 1, 0, size[2] - 1 ) ;
scanConverter->Update () ;
vtkImageData *vtkBinaryVolume = scanConverter->GetBinaryVolume () ;
vtkSmartPointer<vtkImageData> vtkBinaryVolume = scanConverter->GetBinaryVolume () ;
ImagePointer binaryVolume ;
//we can safely can 'value' which is an int to unsigned char because we checked that its value is between 1 and 255 (1 because 0 is the background value)
binaryVolume = VTK2BinaryITK ( vtkBinaryVolume , direction , (unsigned char)value ) ;
Expand Down
42 changes: 12 additions & 30 deletions vtkAttributedPolyDataToImage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ vtkAttributedPolyDataToImage::vtkAttributedPolyDataToImage()
this->BinaryVolume = NULL ;
this->AttributeVolume = NULL ;
this->Attributes = NULL ;
this->faceList = vtkIdTypeArray::New () ;
this->pointList = vtkPoints::New () ;
this->faceList = vtkSmartPointer<vtkIdTypeArray>::New () ;
this->pointList = vtkSmartPointer<vtkPoints>::New () ;
this->ScanConvertPerformed = false ;
this->mesh = NULL ;
this->stencil = NULL ;
Expand All @@ -38,22 +38,7 @@ vtkAttributedPolyDataToImage::vtkAttributedPolyDataToImage()
//----------------------------------------------------------------------------
vtkAttributedPolyDataToImage::~vtkAttributedPolyDataToImage()
{
if ( this->faceList )
this->faceList->Delete () ;
if ( this->pointList )
this->pointList->Delete () ;
if ( this->BinaryVolume )
this->BinaryVolume->Delete () ;

if ( this->AttributeVolume )
this->AttributeVolume->Delete () ;
if ( this->stencil )
{
//this->GetOutput()->Delete() ;
//if ( this->stencil->GetStencil() )
//this->stencil->GetStencil()->Delete () ;
// this->stencil->Delete () ;
}

}

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -114,8 +99,8 @@ int vtkAttributedPolyDataToImage::RequestData(
std::cout << "Origin: " << origin[0] << " " << origin[1] << " " << origin[2] << std::endl ;
std::cout << "Extent: " << extent[0] << " " << extent[1] << " " << extent[2] << " " << extent[3] << " " << extent[4] << " " << extent[5] << std::endl ;
}
vtkOBBTree *tree = this->OBBTree;
vtkPoints *points = vtkPoints::New();
vtkSmartPointer<vtkOBBTree> tree = this->OBBTree;
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();

double p0[3],p1[3];

Expand Down Expand Up @@ -251,7 +236,6 @@ int vtkAttributedPolyDataToImage::RequestData(
{
delete [] zlist;
}
points->Delete();

this->ScanConvertPerformed = true ;
return 1 ;
Expand All @@ -261,13 +245,13 @@ void vtkAttributedPolyDataToImage::ComputeAttributeVolume ()
{
double spacing[3], origin[3] ;
int *extent ;
vtkImageStencilData *stencil = this->GetOutput () ;
vtkSmartPointer<vtkImageStencilData> stencil = this->GetOutput () ;
stencil->GetSpacing ( spacing ) ;
stencil->GetOrigin ( origin ) ;
extent= this->GetOutputWholeExtent () ;

// create empty image
this->AttributeVolume = vtkImageData::New () ;
this->AttributeVolume = vtkSmartPointer<vtkImageData>::New () ;
this->AttributeVolume->SetOrigin ( origin ) ;
this->AttributeVolume->SetSpacing ( spacing ) ;
this->AttributeVolume->SetDimensions ( extent[1] - extent[0] + 1, extent[3] - extent[2] + 1, extent[5] - extent[4] + 1 ) ;
Expand Down Expand Up @@ -296,7 +280,7 @@ void vtkAttributedPolyDataToImage::ComputeAttributeVolume ()
this->pointList->GetPoint ( i, p ) ;
faceId = this->faceList->GetValue ( i ) ;

vtkIdList *facePoints = vtkIdList::New () ;
vtkSmartPointer<vtkIdList> facePoints = vtkSmartPointer<vtkIdList>::New () ;
this->mesh->GetCellPoints ( faceId, facePoints ) ;
result = this->mesh->GetCell ( faceId )->EvaluatePosition ( p, closestPoint, subId, pCoords, dist2, weights ) ;
if( this->GetDebug() )
Expand Down Expand Up @@ -324,12 +308,11 @@ void vtkAttributedPolyDataToImage::ComputeAttributeVolume ()
gridCoords[0]-- ;

this->AttributeVolume->SetScalarComponentFromDouble ( gridCoords[0], gridCoords[1], gridCoords[2], 0, attributeValue );
facePoints->Delete () ;
}
//std::cout << "Attribute volume computed. " << std::endl ;
}

vtkImageData * vtkAttributedPolyDataToImage::GetBinaryVolume()
vtkSmartPointer<vtkImageData> vtkAttributedPolyDataToImage::GetBinaryVolume()
{
if ( !this->ScanConvertPerformed )
return NULL ;
Expand All @@ -346,14 +329,14 @@ vtkImageData * vtkAttributedPolyDataToImage::GetBinaryVolume()
size[2] = extent[5] - extent[4] + 1 ;

// Create an empty image
vtkImageData *emptyImage = vtkImageData::New () ;
vtkSmartPointer<vtkImageData> emptyImage = vtkSmartPointer<vtkImageData>::New () ;
emptyImage->SetOrigin ( origin ) ;
emptyImage->SetSpacing ( spacing ) ;
emptyImage->SetDimensions ( size ) ;
emptyImage->AllocateScalars(VTK_INT, 1);

// Use the stencil as a cookie cutter
this->stencil = vtkImageStencil::New () ;
this->stencil = vtkSmartPointer<vtkImageStencil>::New () ;
this->stencil->SetInputData ( emptyImage ) ;
//this->GetOutput() ;

Expand All @@ -365,12 +348,11 @@ vtkImageData * vtkAttributedPolyDataToImage::GetBinaryVolume()
this->BinaryVolume = this->stencil->GetOutput () ;
//this->stencil->Delete() ;

emptyImage->Delete () ;

return this->BinaryVolume ;
}

vtkImageData * vtkAttributedPolyDataToImage::GetAttributeVolume()
vtkSmartPointer<vtkImageData> vtkAttributedPolyDataToImage::GetAttributeVolume()
{
if ( ( !this->ScanConvertPerformed ) || ( !this->Attributes ) )
return NULL ;
Expand Down
16 changes: 8 additions & 8 deletions vtkAttributedPolyDataToImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class vtkAttributedPolyDataToImage : public vtkPolyDataToImageStencilOBBTree

vtkSetMacro(Attributes, vtkFloatArray *) ;

vtkImageData *GetAttributeVolume () ;
vtkImageData *GetBinaryVolume () ;
vtkSmartPointer<vtkImageData> GetAttributeVolume () ;
vtkSmartPointer<vtkImageData> GetBinaryVolume () ;


protected:
Expand All @@ -48,13 +48,13 @@ class vtkAttributedPolyDataToImage : public vtkPolyDataToImageStencilOBBTree
vtkAttributedPolyDataToImage (const vtkAttributedPolyDataToImage&); // Not implemented.
void operator=(const vtkAttributedPolyDataToImage&); // Not implemented.

vtkImageData *BinaryVolume, *AttributeVolume ;
vtkFloatArray *Attributes ;
vtkSmartPointer<vtkImageData> BinaryVolume, AttributeVolume ;
vtkSmartPointer<vtkFloatArray> Attributes ;

vtkIdTypeArray *faceList ;
vtkPoints *pointList ;
vtkPolyData *mesh ;
vtkImageStencil *stencil ;
vtkSmartPointer<vtkIdTypeArray> faceList ;
vtkSmartPointer<vtkPoints> pointList ;
vtkSmartPointer<vtkPolyData> mesh ;
vtkSmartPointer<vtkImageStencil> stencil ;
typedef vtkImageStencilSource SuperSuperclass ;
bool ScanConvertPerformed ;
} ;
Expand Down
6 changes: 2 additions & 4 deletions vtkPolyDataToImageStencilOBBTree.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void vtkAddEntryToList(int *&clist, int &clistlen, int &clistmaxlen, int r)
}

//----------------------------------------------------------------------------
void vtkTurnPointsIntoList(vtkPoints *points, int *&clist, int &clistlen,
void vtkTurnPointsIntoList(vtkSmartPointer<vtkPoints> points, int *&clist, int &clistlen,
int extent[6], double origin[3], double spacing[3],
int dim)
{
Expand Down Expand Up @@ -257,7 +257,7 @@ int vtkPolyDataToImageStencilOBBTree::RequestData(
double *origin = data->GetOrigin();

vtkOBBTree *tree = this->OBBTree;
vtkPoints *points = vtkPoints::New();
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();

double p0[3],p1[3];

Expand Down Expand Up @@ -371,8 +371,6 @@ int vtkPolyDataToImageStencilOBBTree::RequestData(
{
delete [] zlist;
}
points->Delete();

return 1;
}

Expand Down
3 changes: 2 additions & 1 deletion vtkPolyDataToImageStencilOBBTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <vtkPoints.h>
#include "vtkImageStencilSource.h"
#include "vtkSmartPointer.h"

class vtkPolyData;
class vtkOBBTree;
Expand Down Expand Up @@ -108,7 +109,7 @@ class vtkPolyDataToImageStencilOBBTree : public vtkImageStencilSource
void operator=(const vtkPolyDataToImageStencilOBBTree&); // Not implemented.
};

void vtkTurnPointsIntoList(vtkPoints *points, int *&clist, int &clistlen,
void vtkTurnPointsIntoList(vtkSmartPointer<vtkPoints> points, int *&clist, int &clistlen,
int extent[6], double origin[3], double spacing[3],
int dim);
void vtkAddEntryToList(int *&clist, int &clistlen, int &clistmaxlen, int r);
Expand Down

0 comments on commit 90033a9

Please sign in to comment.