Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Memory Leak with VTK engine.

Featured Replies

Posted

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

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).

  • Author

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.

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.