Jump to content
Tuts 4 You

Memory Leak with VTK engine.


Vic

Recommended Posts

Hi guys,

I need your help. I'm using the VTK engine for my project but it's occurring memory leaking.
It's occurring when go over InsertNextScalarValue function.
Leak about 1~2MB when CSurfaceRenderer::Setup function finished.
I haven't determined the detail cause and I'm looking for a method to fix it. Who has experience about it can help me?

Thanks so much.

std::auto_ptr<CSmoothSurfaceData> m_pSmoothSurfaceData;

bool CSurfaceRenderer::Setup(const CCalcMatrix3D* pCalcMatrix)
{
  int numRows = pCalcMatrix->GetRows();
  int numCols = pCalcMatrix->GetCols();
  int numPlanes = pCalcMatrix->GetNumPlanes();
  const float* pPosX = pCalcMatrix->GetpPosX();
  const float* pPosY = pCalcMatrix->GetpPosY();
  const float* pPosZ = pCalcMatrix->GetpPosZ();

  m_ColSpacing = pCalcMatrix->GetXSpacing();
  m_RowSpacing = pCalcMatrix->GetYSpacing();
  m_OriginX = pCalcMatrix->GetpPosX()[0];
  m_OriginY = pCalcMatrix->GetpPosY()[0];

  // Create smoothing data.
  m_pSmoothSurfaceData.reset(new CSmoothSurfaceData());
  m_pSmoothSurfaceData->SetDataDimensions(numCols, numRows, numPlanes);
  for (int i = 0; i < numCols; i++)
  {
    m_pSmoothSurfaceData->InsertNextXCoord(pPosX[i]);
  }
  for (int i = 0; i < numRows; i++)
  {
    m_pSmoothSurfaceData->InsertNextYCoord(pPosY[i]);
  }
  for (int i = 0; i < numPlanes; i++)
  {
    m_pSmoothSurfaceData->InsertNextZCoord(pPosZ[i]);
  }

  CCalcMatrix3D* pCalcMatrix1 = const_cast<CCalcMatrix3D*>(pCalcMatrix);
  for (short z = 0; z < numPlanes; z++) // numPlanes = 27
  {
    for (short y = 0; y < numRows; y++) // numRows = 54
    {
      for (short x = 0; x < numCols; x++) // numCols = 55
      {
        m_pSmoothSurfaceData->InsertNextScalarValue(pCalcMatrix1->GetDose(x, y, z)); // <- The issue here: InsertNextScalarValue
      }
    }
  }
  
  return true;
}

inline void CSmoothSurfaceData::InsertNextScalarValue(double value)
{
  m_pDataArray->InsertNextValue(value);
}

// Description:
// Insert data at the end of the array. Return its location in the array.
vtkIdType InsertNextValue(double f)
{
  return this->RealSuperclass::InsertNextValue(f);
}

template <class T>
vtkIdType vtkDataArrayTemplate<T>::InsertNextValue(T f)
{
  this->InsertValue(++this->MaxId, f);
  return this->MaxId;
}

 

 

Edited by Vic
C Highlight
Link to comment

I don't know anything about the framework you're using, but const_cast looks wrong to me :D

The memory leak is plain and simple, you do:

m_pSmoothSurfaceData.reset(new CSmoothSurfaceData());

Just make sure to free the old allocation first (no clue how it would work in this case since I don't know the framework).

Link to comment

Thanks Mr. eXoDia for your replay. But fixed it. vtkSmartPointer should be used:

vtkSmartPointer<vtkDoubleArray>m_pDataArray = vtkSmartPointer<vtkDoubleArray>::New();

But my old code was:

vtkSmartPointer<vtkDoubleArray>m_pDataArray = vtkDoubleArray::New();

My old code above didn't reset reference count when renew.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...