I am working on a Windows command line program written in C using Visual Studio Express 2013 for Windows Desktop. When compiling in debug mode, I would really like my program to detect memory leaks and print them by standard error or standard outputs so that they are on my face.
By calling _ CrtDumpMemoryLeaks , I can get the memory leak information printed on the Debug output in Visual Studio (which you can find in the Output Panel section). Based on the MSDN documentation, I think I can add a call to _ CrtSetDumpClient to access the dumped data, and then print to stderr.
Here is the code I use to test this problem:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <stdio.h>
#include <crtdbg.h>
void dumpClient(void * userPortion, size_t blockSize)
{
printf("memory leak: %d\n", blockSize);
}
int main(int argc, char ** argv)
{
printf("hello\n");
_CrtSetDumpClient(&dumpClient);
malloc(44);
_CrtDumpMemoryLeaks();
return 0;
}
Visual Studio ++ Win32 Console Visual Studio, , , , IDE . , F5 ( " " ), Debug Visual Studio, :
Detected memory leaks!
Dumping objects ->
c:\users\david\documents\scraps\test_vc++\testvc\testvc.cpp(15) : {81} normal block at 0x0120A500, 44 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
The program '[3868] TestVC.exe' has exited with code 0 (0x0).
, dumpClient, , . , , hello. , :
hello
memory leak: 44
- , dumpClient ?