In mitkImage.h the method "ImageVtkAccessor* GetVtkImageData(int t = 0, int n = 0);" produces a memory leak.
Code to reproduce this:
for (int i=0; i<10; i++)
{
// Normal way to load an image QString stringImage("c:/image.nrrd"); mitk::DataNode::Pointer nodeImage = CommonFunctionality::FileOpen( stringImage ); mitk::Image::Pointer image = dynamic_cast<mitk::Image*>(nodeImage->GetData()); // This function allocates memory and never releases it, even if the mitkImage is already destroyed image->GetVtkImageData()
}
The cause is here:
in the function "mitk::ImageVtkAccessor* mitk::Image::GetVtkImageData(int t, int n)" there is a line
ImageDataItemPointer volume=GetVolumeData(t, n);
This creates an itk Smartpointer object called "volume" which points to a specific volume of the image. This volume object is created, but never deleted. At this time, the object "volume" is not very big. But some lines later there is a call
volume->GetVtkImageData(this).
This call causes the "volume" object to fill it's m_VtkImageData pointer with a lot of image data. "volume" would delete and free that memory in it's destructor. But the destructor of "volume" is never called. I am not sure why... Seems like the Itk Smartpointer is not working properly or there are cross-references which prevents the deletion.
Any ideas?