How to use _CrtDumpMemoryLeaks()

Download the sample from the following link. You have to set the following parameters to direct output to console.

   // Send all reports to STDOUT
   _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
   _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
   _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
   _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
   _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
   _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );

After searching why it does not work in my code I found the following points:

  • If there is no detected leak then this method prints nothing.
  • Not all allocation methods are affected. For example CoTaskMemAlloc are not affected.
  • The warning must be enabled with _CrtSetReportMode and _CrtSetReportFile.