[vtkusers] Delete vtkobjects properly in MFC

Andreas Brüning mail-andi at web.de
Fri Aug 1 04:40:29 EDT 2008


Hi,

thank you for your answer. Of course you are right with this this line in the end my code. I tested something and it remains in there, sorry.
When i take all the vtk stuff out of my project i dont have leaks anymore. As you said i tried to isolate the vtk objects to see which one leaks. Therefore i wrote a small class which only contains pointer of vtkobjects i want to test. 

i only worte a constructor and destructor like this:

ImageVolumeData::ImageVolumeData(void)
{
	vtk_importer = vtkImageImport::New(); // test vtk objects
        vtkflipper = vtkImageFlip::New();	
}

ImageVolumeData::~ImageVolumeData(void)
{
	vtkflipper->Delete();
 	vtk_importer->Delete();	 
}

Than i created an object of this class and delete it immediately after the creation to see if the memory is freed. 


ImageVolumeData *test;

test = new ImageVolumeData();
delete test;


Now comes the point where i really get stucked. The allocated memory is not freed after i delete the object. I checked it with the Windows Task Manager while i debuged the code.(Can this maybe wrong?)
Another strange thing is when i do this prcedure twice or more times like:

ImageVolumeData *test;

test = new ImageVolumeData();
delete test;
test = new ImageVolumeData();
delete test;
test = new ImageVolumeData();
delete test;

it only allocates memory the first time i create the object. I can test any vtk object it is allways the same.  When i close the program no memoryleaks are detected but i want to create and delete vtkobjects during the runtime and not close the program all the time. 
In my opinion the error is not in VTK. it is maybe a wrong setting in CMake or in the projects properties. But i dont have much experience with this and really need help to cleanup the memory.   

Does anybody know what is the mistake?
Andi




You shouldn't do this:
 vtk_extractor->SetInput(pThis->m_vtkvolumedata);

...after a vtk_extractor->Delete() call...


...maybe you meant to "SetInput(NULL)" *before* deleting it...?


Try removing that SetInput call at the bottom of your proc to see if 
things improve at all.


Next, I would try eliminating the VTK objects one by one to try to 
isolate which one is causing the leak (if any)... If you eliminate 
them all and still have a leak, then look to your surrounding code. 
Otherwise, let us know if you find the leak in VTK code at all. If 
you can isolate it, we can fix it.




Thx,
David




 On Thu, Jul 31, 2008 at 10:37 AM, Andi2008 <mail-andi at web.de> wrote:

 ok, here is a vtk part of my code. I manage different vtk pipelines 
in
 different threads. an object of class CImageVtkWnd manages the 
different
 threads. I use an offscreen rendering and create screenshots of the 
result.
 I also have the leaks when use Onscreenrendering.

 This is the methode i call in one thread

 UINT __cdecl CImageVtkWnd::VTK3DProc(LPVOID lpParameter)
 {


 CImageVtkWnd *pThis = static_cast<CImageVtkWnd *>(lpParameter);

 ASSERT(pThis != NULL);

 //build the RenderPipeline

 vtkRenderer *vtk_Renderer = vtkRenderer::New();
 vtkWin32OpenGLRenderWindow *vtk_Win32OpenGLWindow =
 vtkWin32OpenGLRenderWindow::New();
 vtkWin32RenderWindowInteractor *vtk_interactor =
 vtkWin32RenderWindowInteractor::New();


 vtk_Win32OpenGLWindow->AddRenderer(vtk_Renderer);
 vtk_Win32OpenGLWindow->OffScreenRenderingOn();


 vtkContourFilter *vtk_extractor = vtkContourFilter::New();


 vtk_extractor->SetInput(pThis->m_vtkvolumedata); //vtkImageData from
 vtkImport
 vtk_extractor->SetValue(0, 350);
 vtk_extractor->ReleaseDataFlagOn();


 vtkStripper *vtk_stripper = vtkStripper::New();
 vtk_stripper->SetInputConnection(vtk_extractor->GetOutputPort());


 vtkPolyDataMapper *vtk_3dmapper = vtkPolyDataMapper::New();
 vtk_3dmapper->SetInputConnection(vtk_stripper->GetOutputPort());
 vtk_3dmapper->ScalarVisibilityOff();



 vtkActor *vtk_3dActor = vtkActor::New();

 vtk_3dActor->SetMapper(vtk_3dmapper);
 vtk_3dActor->GetProperty()->SetDiffuseColor(0.9, 0.9, 0.9);

 vtk_Renderer->AddActor(vtk_3dActor);
 vtk_Renderer->ResetCamera();


 vtk_Win32OpenGLWindow->SetSize(512,512);
 vtk_interactor->SetRenderWindow(vtk_Win32OpenGLWindow);

 vtk_Renderer->ResetCameraClippingRange();


 vtkInteractorStyleTrackballCamera *vtk_TrackBallCam =
 vtkInteractorStyleTrackballCamera::New();

 vtk_interactor->SetInteractorStyle(vtk_TrackBallCam);
 vtk_interactor->Initialize();

 CSyncObject* pWaitObjects[] = { pThis->m_hRenderEvent,
 pThis->m_hTerminateLoopEvent, pThis->m_hLButtonUp, pThis->m_
hLButtonDown,
 pThis->m_hRButtonUp, pThis->m_hRButtonDown, pThis->m_hMouseMove};

 CMultiLock MultiLock( pWaitObjects, 7L );


 DWORD MultiLockObj = 0;

 int *size = vtk_Win32OpenGLWindow->GetSize();


 BITMAPINFO bmi={0};
 bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
 bmi.bmiHeader.biPlanes=1;
 bmi.bmiHeader.biCompression=BI_RGB;
 bmi.bmiHeader.biBitCount=24;
 bmi.bmiHeader.biWidth=size[0];
 bmi.bmiHeader.biHeight=size[1];
 bmi.bmiHeader.biSizeImage=size[0] * size[1] * 24;



 unsigned char *tmpdata;
 bool recalcRect=true;

 do
 {
 if(MultiLockObj == (WAIT_OBJECT_0+1))
 {
 break;
 }

 if(MultiLockObj == (WAIT_OBJECT_0+2))
 {

 vtk_interactor->OnLButtonUp(vtk_Win32OpenGLWindow->GetWindowId(),0,
pThis->m_pMouseDelta.x,pThis->m_pMouseDelta.y);
 }


 if(MultiLockObj == (WAIT_OBJECT_0+3))
 {

 vtk_interactor->OnLButtonDown(vtk_Win32OpenGLWindow->GetWindowId(),0,
pThis->m_pMouseDelta.x,pThis->m_pMouseDelta.y);
 }

 if(MultiLockObj == (WAIT_OBJECT_0+4))
 {

 vtk_interactor->OnRButtonUp(vtk_Win32OpenGLWindow->GetWindowId(),0,
pThis->m_pMouseDelta.x,pThis->m_pMouseDelta.y);
 }
 if(MultiLockObj == (WAIT_OBJECT_0+5))
 {

 vtk_interactor->OnRButtonDown(vtk_Win32OpenGLWindow->GetWindowId(),0,
pThis->m_pMouseDelta.x,pThis->m_pMouseDelta.y);
 }

 if(MultiLockObj == (WAIT_OBJECT_0+6))
 {

 vtk_interactor->OnMouseMove(vtk_Win32OpenGLWindow->GetWindowId(),0,
pThis->m_pMouseDelta.x,pThis->m_pMouseDelta.y);
 }




 vtk_Win32OpenGLWindow->Render();

 ::EnterCriticalSection(&pThis->m_cs);

 tmpdata = vtk_Win32OpenGLWindow->GetPixelData(0,0,size[0]-1,size[1]-
1,1);


 pThis->m_lbitmap->ConvertFromDIB(&bmi,(L_UCHAR *)tmpdata);
 pThis->m_lbitmap->SetXResolution(pThis->m_iSource_resolution);
 pThis->m_lbitmap->SetYResolution(pThis->m_iSource_resolution);
 pThis->m_lbitmap->CalculateXYRes();

 LeaveCriticalSection(&pThis->m_cs);


 ::PostMessage(pThis->m_cParent->GetSafeHwnd(),WM_VTK_UPDATE_WINDOW,0,
NULL);


 delete tmpdata;

 recalcRect=false;



 MultiLockObj = MultiLock.Lock( INFINITE, FALSE );
 }
 while(1) ;




 //Cleanup

 vtk_extractor->Delete();
 vtk_stripper->Delete();
 vtk_3dmapper->Delete();
 vtk_3dActor->Delete();


 vtk_Renderer->Delete();
 vtk_Win32OpenGLWindow->Finalize();
 vtk_Win32OpenGLWindow->Delete();



 vtk_TrackBallCam->Delete();
 vtk_interactor->Delete();

 vtk_extractor->SetInput(pThis->m_vtkvolumedata);

 ::AfxEndThread( 0, true );
 return 0L;
 }


 Greetings
 Andi



____________________________________________________________________
Ihre Messenger, Communities und E-Mails jetzt in einem Programm!
WEB.DE MultiMessenger http://www.produkte.web.de/messenger/?did=3071




More information about the vtkusers mailing list