Commit 7fa22c7a authored by Will Schroeder's avatar Will Schroeder
Browse files

ENH: Improved memory management scheme.

parent ab4e1e8a
......@@ -22,6 +22,7 @@ Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994
#ifndef vtkIdList_h
#define vtkIdList_h
#include "Object.hh"
#include "IntArray.hh"
class vtkIdList {
......
......@@ -30,8 +30,9 @@ Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994
class vtkObject
{
public:
vtkObject();
virtual ~vtkObject();
vtkObject(); //create a vtk object
virtual void Delete(); //delete a vtk object.
virtual ~vtkObject(); //use Delete() whenever possible
virtual char *GetClassName() {return "vtkObject";};
// debugging
......
......@@ -20,6 +20,12 @@ Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994
// uses them. Once the last reference to a reference counted object is
// removed, the object will spontaneously destruct. Typically only data
// objects that are passed between objects are reference counted.
// Note: in vtk objects are generally created with combinations of
// new/Delete() methods. This works great until you want to allocate
// objects off the stack (i.e., automatic objects). Automatic objects
// when automatically deleted (by exiting scope) will cause warnings to
// occur. You can avoid this by turing reference counting off (i.e., use
// the method ReferenceCountingOff()).
#ifndef __vtkRefCount_hh
#define __vtkRefCount_hh
......@@ -30,6 +36,7 @@ class vtkRefCount : public vtkObject
{
public:
vtkRefCount();
void Delete();
~vtkRefCount();
void PrintSelf(ostream& os, vtkIndent indent);
char *GetClassName() {return "vtkRefCount";};
......@@ -37,10 +44,22 @@ public:
void Register(vtkObject* o);
void UnRegister(vtkObject* o);
int GetRefCount() {return this->RefCount;};
void ReferenceCountingOff();
private:
int RefCount; // Number of uses of this object by other objects
int ReferenceCounting; // Turn on/off reference counting mechanism
};
// Description:
// Turn off reference counting for this object. This allows you to create
// automatic reference counted objects and avoid warning messages when scope
// is existed. (Note: It is preferable to use the combination new/Delete()
// to create and delete vtk objects).
inline void vtkRefCount::ReferenceCountingOff()
{
this->ReferenceCounting = 0;
}
#endif
......@@ -163,13 +163,15 @@ void vtkBooleanStructuredPoints::InitializeBoolean()
if ( inScalars != NULL )
{
newScalars = inScalars->MakeObject(numPts);
this->PointData.SetScalars(newScalars);
newScalars->Delete();
}
else
{
newScalars = new vtkFloatScalars(numPts);
this->PointData.SetScalars(newScalars);
}
this->PointData.SetScalars(newScalars);
}
// Perform Boolean operations on input volumes
......
......@@ -35,8 +35,8 @@ void vtkDataSetToDataSetFilter::Initialize()
{
if ( this->Input )
{
delete this->DataSet;
// copies input geometry to internal data set
this->DataSet->Delete();
this->DataSet = this->Input->MakeObject();
}
else
......
......@@ -150,6 +150,7 @@ void vtkDecimate::Execute()
Mesh->SetPoints(inPts);
newPolys = new vtkCellArray(*(inPolys));
Mesh->SetPolys(newPolys);
newPolys->Delete(); //registered by Mesh and preserved
Mesh->BuildLinks();
//
// Create array of vertex errors (initially zero)
......@@ -372,13 +373,16 @@ void vtkDecimate::CreateOutput(int numPts, int numTris, int numEliminated,
}
delete [] map;
delete Mesh; //side effect: releases memory consumned by data structures
Mesh->Delete();
this->SetPoints(newPts);
this->SetPolys(newPolys);
newPts->Delete();
newPolys->Delete();
if ( this->GenerateErrorScalars )
{
this->PointData.SetScalars(newScalars);
newScalars->Delete();
delete [] VertexError;
}
}
......
......@@ -115,8 +115,8 @@ void vtkMergeFilter::Initialize()
{
if ( this->Geometry )
{
delete this->Geometry;
// copies input geometry to internal data set
this->Geometry->Delete();
this->Geometry = this->Geometry->MakeObject();
}
else
......
......@@ -3,8 +3,8 @@
Program: Visualization Toolkit
Module: Object.cc
Language: C++
Date: $Date$
Version: $Revision$
Date: 30 Jun 1995
Version: 1.19
This file is part of the Visualization Toolkit. No part of this file or its
contents may be copied, reproduced or altered in any way without the express
......
......@@ -69,8 +69,8 @@ void vtkProbeFilter::Initialize()
{
if ( this->Source )
{
if (this->DataSet) delete this->DataSet;
// copies SOURCE geometry to internal data set
if (this->DataSet) this->DataSet->Delete();
this->DataSet = this->Source->MakeObject();
}
}
......
......@@ -225,6 +225,7 @@ void vtkPointData::CopyAllocate(vtkPointData* pd, int sze, int ext)
else newScalars = s->MakeObject(s->GetNumberOfScalars());
newScalars->SetLookupTable(s->GetLookupTable());
this->SetScalars(newScalars);
newScalars->Delete();
}
if ( this->CopyVectors && (v = pd->GetVectors()) )
......@@ -232,6 +233,7 @@ void vtkPointData::CopyAllocate(vtkPointData* pd, int sze, int ext)
if ( sze > 0 ) newVectors = v->MakeObject(sze,ext);
else newVectors = v->MakeObject(v->GetNumberOfVectors());
this->SetVectors(newVectors);
newVectors->Delete();
}
if ( this->CopyNormals && (n = pd->GetNormals()) )
......@@ -239,6 +241,7 @@ void vtkPointData::CopyAllocate(vtkPointData* pd, int sze, int ext)
if ( sze > 0 ) newNormals = n->MakeObject(sze,ext);
else newNormals = n->MakeObject(n->GetNumberOfNormals());
this->SetNormals(newNormals);
newNormals->Delete();
}
if ( this->CopyTCoords && (t = pd->GetTCoords()) )
......@@ -246,6 +249,7 @@ void vtkPointData::CopyAllocate(vtkPointData* pd, int sze, int ext)
if ( sze > 0 ) newTCoords = t->MakeObject(sze,t->GetDimension(),ext);
else newTCoords = t->MakeObject(t->GetNumberOfTCoords(),t->GetDimension());
this->SetTCoords(newTCoords);
newTCoords->Delete();
}
if ( this->CopyTensors && (tens = pd->GetTensors()) )
......@@ -253,6 +257,7 @@ void vtkPointData::CopyAllocate(vtkPointData* pd, int sze, int ext)
if ( sze > 0 ) newTensors = tens->MakeObject(sze,tens->GetDimension(),ext);
else newTensors = tens->MakeObject(tens->GetNumberOfTensors(),tens->GetDimension());
this->SetTensors(newTensors);
newTensors->Delete();
}
if ( this->CopyUserDefined && (ud = pd->GetUserDefined()) )
......@@ -260,6 +265,7 @@ void vtkPointData::CopyAllocate(vtkPointData* pd, int sze, int ext)
if ( sze > 0 ) newUserDefined = ud->MakeObject(sze,ext);
else newUserDefined = ud->MakeObject(ud->GetNumberOfUserDefined());
this->SetUserDefined(newUserDefined);
newUserDefined->Delete();
}
};
......
......@@ -215,11 +215,16 @@ void vtkPointLoad::Execute()
}
}
//
// Update self
// Update self and free memory
//
this->PointData.SetTensors(newTensors);
newTensors->Delete();
if ( this->ComputeEffectiveStress)
{
this->PointData.SetScalars(newScalars);
newScalars->Delete();
}
}
......
......@@ -79,9 +79,9 @@ void vtkPointSetToPointSetFilter::Initialize()
{
if ( this->Input != NULL )
{
vtkDataSet *ds=this->Input->MakeObject();
delete this->PointSet;
// copies input geometry to internal data set
vtkDataSet *ds=this->Input->MakeObject();
this->PointSet->Delete();
this->PointSet = ds;
}
else
......
......@@ -16,15 +16,35 @@ Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994
#include "RefCount.hh"
// Description:
// Construct with initial reference count = 0.
// Construct with initial reference count = 1 and reference counting on.
vtkRefCount::vtkRefCount()
{
this->RefCount = 0;
this->RefCount = 1;
this->ReferenceCounting = 0;
}
// Description:
// Overload vtkObject's Delete() method. For reference counted objects the
// Delete() method simply unregisters the use of the object. This may or
// may not result in the destruction of the object depending upon whether
// another object is referencing it.
void vtkRefCount::Delete()
{
this->UnRegister((vtkObject *)NULL);
}
// Description:
// Destructor for reference counted objects. Reference counted objects should
// almost always use the combination of new/Delete() to create and delete
// objects. Automatic reference counted objects (i.e., creating them on the
// stack) are not encouraged. However, if you desire to this, you will have to
// use the ReferenceCountingOff() method to avoid warning messages when the
// objects are automatically deleted upon scope termination.
vtkRefCount::~vtkRefCount()
{
if (this->RefCount > 0)
// warn user if reference counting is on and the object is being referenced
// by another object
if ( this->RefCount > 0 && this->ReferenceCounting )
{
vtkErrorMacro(<< "Trying to delete object with non-zero reference count");
}
......@@ -42,7 +62,7 @@ void vtkRefCount::Register(vtkObject* o)
// Decrease the reference count (release by another object).
void vtkRefCount::UnRegister(vtkObject* o)
{
vtkDebugMacro(<< "UnRegistered by " << o->GetClassName() << " (" << 0 << ")");
vtkDebugMacro(<< "UnRegistered by " <<o->GetClassName() << " (" << 0 << ")");
if (--this->RefCount <= 0) delete this;
}
......@@ -52,4 +72,5 @@ void vtkRefCount::PrintSelf(ostream& os, vtkIndent indent)
vtkObject::PrintSelf(os,indent);
os << indent << "Reference Count: " << this->RefCount << "\n";
os << indent << "Reference Counting: "<< (this->ReferenceCounting ? "On\n" : "Off\n");
}
......@@ -17,12 +17,12 @@ Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994
void vtkShrinkFilter::Execute()
{
vtkIdList ptIds(MAX_CELL_SIZE), newPtIds(MAX_CELL_SIZE);
vtkFloatPoints *newPts;
int i, j, cellId, numCells, numPts;
int oldId, newId;
float center[3], *p, pt[3];
vtkPointData *pd;
vtkIdList ptIds(MAX_CELL_SIZE), newPtIds(MAX_CELL_SIZE);
vtkDebugMacro(<<"Shrinking cells");
this->Initialize();
......@@ -71,11 +71,12 @@ void vtkShrinkFilter::Execute()
this->InsertNextCell(this->Input->GetCellType(cellId), newPtIds);
}
//
// Update ourselves
// Update ourselves and release memory
//
this->SetPoints(newPts);
this->Squeeze();
newPts->Delete();
}
void vtkShrinkFilter::PrintSelf(ostream& os, vtkIndent indent)
......
......@@ -164,6 +164,7 @@ void vtkTransformStructuredPoints::Execute()
// Update ourselves
this->PointData.SetScalars(outScalars);
outScalars->Delete();
}
......
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