Memory leaks istanziating itkImageFileReader in MFC

I just tried your code (with commenting out stdafx.h):

//#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#include "itkImageFileReader.h"
#include "itkMetaImageIOFactory.h"

using namespace itk;

int
main()
{
  const char *                             metaImageFileName = "C:/a/image_corrUC.mha";
  constexpr unsigned int                   Dimension = 3;
  typedef unsigned short                   PixelType;
  typedef itk::Image<PixelType, Dimension> ImageType;
  typedef itk::ImageFileReader<ImageType>  ReaderType;

  ReaderType::Pointer reader = ReaderType::New();
  using RegisteredObjectsContainerType = std::list<itk::LightObject::Pointer>;

  RegisteredObjectsContainerType registeredIOs = itk::ObjectFactoryBase::CreateAllInstance("itkImageIOBase");

  reader->SetFileName(metaImageFileName);
  itk::MetaImageIOFactory::RegisterOneFactory();

  try
  {
    reader->Update();
    std::cout << "succeed.\n" << std::endl;
  }
  catch (itk::ImageFileReaderException & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }
  system("PAUSE");
  _CrtDumpMemoryLeaks();

  return 0;
}

… and I get no memory leaks:

succeed.

Press any key to continue . . .
Press any key to continue . . .

If I don’t comment the first line, I get: C:\Misc\Tester\tester.cpp(1): fatal error C1083: Cannot open include file: ‘stdafx.h’: No such file or directory

Microsoft Visual Studio Community 2015 Version 14.0.25431.01 Update 3
Windows 10 x64 Version 19.03
ITK-5.0.1 (built with static libraries - I don’t know whether that matters)
CMake 3.15.5 (but CMake version probably doesn’t matter)

Hi Dzenan,
tks! I don’t know why I still have the memory leaks.
Just one difference (I don’t know if it is a big difference), ITK 5.0.1 has been built has shared libraries (.dlls).
Could you please try this way?
Tks

This looks like a good blog on using this tool:

Note:

False positives
_CrtDumpMemoryLeaks can give false indications of memory leaks if a library marks internal allocations as normal blocks instead of CRT blocks or client blocks. In that case, _CrtDumpMemoryLeaks is unable to tell the difference between user allocations and internal library allocations. If the global destructors for the library allocations run after the point where you call _CrtDumpMemoryLeaks, every internal library allocation is reported as a memory leak. Versions of the Standard Template Library earlier than Visual Studio .NET may cause _CrtDumpMemoryLeaks to report such false positives.

At the point _CrtDumpMemoryLeaks() is run, even the reader smart pointer is not released much less dynamic library resources. I don’t think this tool will easily work with shared libraries and the complicated order of when static objects are destroyed across libraries.

I don’t think you are detecting a real problem.

2 Likes

With VS2019, and ITK-master af89589, built with shared libraries, I get no leaks either:

BMPImageIO : Magic Number Fails = O : b
succeed.

Press any key to continue . . .

C:\Misc\Tester\Debug\tester.exe (process 37516) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
1 Like

Hi Dzenan,
I am attaching a sample application.

Now I don’t use anymore a Win 32 Console Application but a single document MFC Application for x64.
In this way I don’t call _CrtDumpMemoryLeaks(); MFCApplication.zip (135.0 KB)

I have Windows 10 1903 and ITK 5.0.1 (the source .zip file downloaded from here Download | ITK) built using CMake 3.15.3 for x64 platform using default options, the only custom option I used was to build as shared library and not as static library .

If you build the application, run and close in debug mode (of course) you should see the memory leaks I’m still having. Use the debug x64 for building the application.

In order to get the memory leaks I just added

#include “itkImageFileReader.h”

in the stdafx.h and I used this code

typedef ImageFileReader <Image<unsigned short, 3>> ReaderType;
ReaderType::Pointer reader = ReaderType::New();

in the method

BOOL CMFCApplicationDoc::OnNewDocument()

In a previous answer you told me that you used Visual Studio 2015 community, so could you please try this application and tell me if you get the same memory leaks?

Tks a lot,
Lorenzo

P.S. naturally you have to change the directories (c++ and .lib) in the project settings.