[vtkusers] Delete vtkobjects properly in MFC

Andi2008 mail-andi at web.de
Thu Jul 31 10:37:41 EDT 2008


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




David Cole wrote:
> 
> Send a code snippet that demonstrates the leak you are talking about.
> Sounds
> like there is a leak internal to a class that does not get cleaned up even
> though its containing object does get properly cleaned up....
> 
> Thx,
> David
> 
> 
> On Thu, Jul 31, 2008 at 10:11 AM, Andreas Brüning <mail-andi at web.de>
> wrote:
> 
>> Hi everyone,
>>
>> i need help to clean up my application. I visualize some medical data and
>> i
>> am using C++,MFC and VS2005. My vtk version is a nightly version from
>> around
>> a week ago. By the way, regarding to my post on July/29, i solved the
>> problem with memory leaks without creating any vtk objects by loading all
>> vtk dll delayed. (strange MFC behavoir )
>>
>> My new problem is that i canŽt clean up my created vtk objects properly. 
>> I
>> call the Delete method for all my vtk objects and when i debug my code
>> all
>> vtk objects are deleted, but the memory isnŽt completely freed. If i
>> handle
>> bigger datasets it leaks more. I also checked twice if forget to call a
>> Delete method a vtk object ;-)
>>
>> Even if i create a object like maybe vtkImport (because it seems to be a
>> quite big object) and call the Delete method immediately after it, the
>> object is deleted but the memory isnŽt freed.
>>
>> I googled already half a day and i don't want to restart my programm all
>> the time if i visualize another dataset. Did anybody had the same thing
>> or
>> know want can be wrong? I think it could be another wrong project
>> property
>> setting or maybe i use CMake with wrong settings.
>>
>> I could really need help. Thank in advance
>> Andi
> 
>> _______________________________________________
>> This is the private VTK discussion list.
>> Please keep messages on-topic. Check the FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>>
> 
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
> 
> 

-- 
View this message in context: http://www.nabble.com/Delete-vtkobjects-properly-in-MFC-tp18755524p18756045.html
Sent from the VTK - Users mailing list archive at Nabble.com.




More information about the vtkusers mailing list