pankajt
pankajt

Reputation: 7864

How to use _CrtDumpMemoryLeaks()

I am trying to use _CrtDumpMemoryLeaks() to display memory leaks in my program.

But it does not display anything except for returning 0 in case of no memory leaks and 1 in case there is a leak.

The link here shows the output should be like:

Detected memory leaks!
Dumping objects ->
D:\VisualC++\CodeGuru\MemoryLeak\MemoryLeak.cpp(67) : {60} 
normal block at 0x00324818, 4 bytes long.
Data: <,   > 2C 00 00 00 
Object dump complete.

Can anyone suggest the correct way of using this function.

Upvotes: 11

Views: 24931

Answers (2)

Horcrux7
Horcrux7

Reputation: 24447

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.

Upvotes: 4

pankajt
pankajt

Reputation: 7864

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 );

Upvotes: 21

Related Questions