Commit c79ca6b3 authored by Will Schroeder's avatar Will Schroeder
Browse files

Dealing with memory leak issues

parent fc96d38a
......@@ -194,6 +194,5 @@ int TestStaticCellLinks( int, char *[] )
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
......@@ -58,8 +58,8 @@ public:
static int GetIdType(vtkIdType maxPtId, vtkIdType maxCellId, vtkCellArray *ca);
protected:
vtkAbstractCellLinks();
~vtkAbstractCellLinks();
vtkAbstractCellLinks();
virtual ~vtkAbstractCellLinks();
private:
vtkAbstractCellLinks(const vtkAbstractCellLinks&); // Not implemented.
......
......@@ -22,6 +22,27 @@
vtkStandardNewMacro(vtkCellLinks);
//----------------------------------------------------------------------------
vtkCellLinks::~vtkCellLinks()
{
this->Initialize();
}
//----------------------------------------------------------------------------
void vtkCellLinks::Initialize()
{
if ( this->Array != NULL )
{
for (vtkIdType i=0; i<=this->MaxId; i++)
{
delete [] this->Array[i].cells;
}
delete [] this->Array;
this->Array = NULL;
}
}
//----------------------------------------------------------------------------
void vtkCellLinks::Allocate(vtkIdType sz, vtkIdType ext)
{
......@@ -39,22 +60,6 @@ void vtkCellLinks::Allocate(vtkIdType sz, vtkIdType ext)
}
}
//----------------------------------------------------------------------------
vtkCellLinks::~vtkCellLinks()
{
if ( this->Array == NULL )
{
return;
}
for (vtkIdType i=0; i<=this->MaxId; i++)
{
delete [] this->Array[i].cells;
}
delete [] this->Array;
}
//----------------------------------------------------------------------------
// Allocate memory for the list of lists of cell ids.
void vtkCellLinks::AllocateLinks(vtkIdType n)
......@@ -128,6 +133,9 @@ void vtkCellLinks::BuildLinks(vtkDataSet *data)
vtkIdType cellId;
unsigned short *linkLoc;
// Make sure that we clear out previous allocation
this->Initialize();
// fill out lists with number of references to cells
linkLoc = new unsigned short[numPts];
memset(linkLoc, 0, numPts*sizeof(unsigned short));
......@@ -209,6 +217,9 @@ void vtkCellLinks::BuildLinks(vtkDataSet *data, vtkCellArray *Connectivity)
vtkIdType *pts=0;
vtkIdType loc = Connectivity->GetTraversalLocation();
// Make sure that we clear out previous allocation
this->Initialize();
// traverse data to determine number of uses of each point
for (Connectivity->InitTraversal();
Connectivity->GetNextCell(npts,pts);)
......
......@@ -63,6 +63,10 @@ public:
// will be built.
void Allocate(vtkIdType numLinks, vtkIdType ext=1000);
// Description:
// Clear out any previously allocated data structures
void Initialize();
// Description:
// Get a link structure given a point id.
Link &GetLink(vtkIdType ptId) {return this->Array[ptId];};
......@@ -149,6 +153,7 @@ protected:
vtkIdType MaxId; // maximum index inserted thus far
vtkIdType Extend; // grow array by this point
Link *Resize(vtkIdType sz); // function to resize data
private:
vtkCellLinks(const vtkCellLinks&); // Not implemented.
void operator=(const vtkCellLinks&); // Not implemented.
......
......@@ -28,21 +28,12 @@ vtkStaticCellLinks::vtkStaticCellLinks()
this->Impl = new vtkStaticCellLinksTemplate<vtkIdType>;
}
//----------------------------------------------------------------------------
vtkStaticCellLinks::~vtkStaticCellLinks()
{
delete this->Impl;
}
//----------------------------------------------------------------------------
// Build the link list array.
void vtkStaticCellLinks::BuildLinks(vtkDataSet *data)
{
this->Impl->BuildLinks(data);
}
//----------------------------------------------------------------------------
void vtkStaticCellLinks::PrintSelf(ostream& os, vtkIndent indent)
{
......
......@@ -60,7 +60,8 @@ public:
// Description:
// Build the link list array. Satisfy the superclass API.
virtual void BuildLinks(vtkDataSet *data);
virtual void BuildLinks(vtkDataSet *ds)
{this->Impl->BuildLinks(ds);}
// Description:
// Get the number of cells using the point specified by ptId.
......@@ -78,10 +79,14 @@ public:
const vtkIdType *GetCells(vtkIdType ptId)
{return this->Impl->GetCells(ptId);}
// Description:
// Make sure any previously created links are cleaned up.
void Initialize()
{this->Impl->Initialize();}
protected:
vtkStaticCellLinks();
~vtkStaticCellLinks();
virtual ~vtkStaticCellLinks();
vtkStaticCellLinksTemplate<vtkIdType> *Impl;
......
......@@ -42,15 +42,14 @@
#ifndef vtkStaticCellLinksTemplate_h
#define vtkStaticCellLinksTemplate_h
#include "vtkAbstractCellLinks.h"
class vtkDataSet;
class vtkPolyData;
class vtkUnstructuredGrid;
class vtkCellArray;
template <typename TIds>
class vtkStaticCellLinksTemplate : public vtkAbstractCellLinks
class vtkStaticCellLinksTemplate
{
public:
// Description:
......@@ -60,20 +59,6 @@ public:
{
}
// Description:
// Release memory if necessary.
~vtkStaticCellLinksTemplate()
{
if ( this->Links )
{
delete [] this->Links;
}
if ( this->Offsets )
{
delete [] this->Offsets;
}
}
// Description:
// Build the link list array. Satisfy superclass' API.
virtual void BuildLinks(vtkDataSet *ds);
......@@ -86,6 +71,10 @@ public:
// Build the link list array for vtkUnstructuredGrid.
void BuildLinks(vtkUnstructuredGrid *ugrid);
// Description:
// Make sure any previously created links are cleaned up.
void Initialize();
// Description:
// Get the number of cells using the point specified by ptId.
TIds GetNumberOfCells(vtkIdType ptId)
......
......@@ -22,6 +22,22 @@
#include "vtkPolyData.h"
#include "vtkUnstructuredGrid.h"
//----------------------------------------------------------------------------
// Clean up any previously allocated memory
template <typename TIds> void vtkStaticCellLinksTemplate<TIds>::
Initialize()
{
if ( this->Links )
{
delete [] this->Links;
this->Links = NULL;
}
if ( this->Offsets )
{
delete [] this->Offsets;
this->Offsets = NULL;
}
}
//----------------------------------------------------------------------------
// Build the link list array for any dataset type
......@@ -41,6 +57,8 @@ BuildLinks(vtkDataSet *ds)
// Any other type of dataset. Generally this is not called as datasets have
// their own, more efficient ways of getting similar information.
// Make sure that we clear out previous allocation.
this->Initialize();
this->NumCells = ds->GetNumberOfCells();
this->NumPts = ds->GetNumberOfPoints();
......@@ -99,6 +117,9 @@ BuildLinks(vtkDataSet *ds)
template <typename TIds> void vtkStaticCellLinksTemplate<TIds>::
BuildLinks(vtkUnstructuredGrid *ugrid)
{
// Make sure that we clear out previous allocation.
this->Initialize();
// Basic information about the grid
this->NumCells = ugrid->GetNumberOfCells();
this->NumPts = ugrid->GetNumberOfPoints();
......@@ -162,6 +183,9 @@ BuildLinks(vtkUnstructuredGrid *ugrid)
template <typename TIds> void vtkStaticCellLinksTemplate<TIds>::
BuildLinks(vtkPolyData *pd)
{
// Make sure that we clear out previous allocation.
this->Initialize();
// Basic information about the grid
this->NumCells = pd->GetNumberOfCells();
this->NumPts = pd->GetNumberOfPoints();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment