I am working on a Windows command-line program written in C using Visual Studio Express 2013 for Windows Desktop. When compiled in Debug mode, I would really like my program to detect memory leaks and print them on the standard error or standard output so they are in my face.
By calling _CrtDumpMemoryLeaks, I am able to get memory leak information printed out to the Debug output in Visual Studio (which you can find under the Output pane). Based on the MSDN documentation, I would think that I can add a call to _CrtSetDumpClient in order to get access to the data being dumped and then print it to stderr.
Here is the code I am using to test this issue:
#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;
}
I made a new Visual C++ Win32 Console Application project in Visual Studio, stuck this code into the project, disabled precompiled headers, made sure that the IDE was in Debug mode, and built. If I run it by pressing F5 (the Start Debugging command), then I can see the following output in the Debug window of Visual Studio, which is good:
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).
However, if I set a breakpoint inside dumpClient
, I can see that it is never being called. Also, if I run the program from a Command Prompt, it just prints hello
. The expected output that I would hope to see is:
hello
memory leak: 44
Does anyone know why the dumpClient
function is not being called at all?