I’m using _CrtDumpMemoryLeaks(); from the stdlib.h and crtdbg.h to detect memory leaks, but I noticed something strange in my code.
If I do:
int _tmain(int argc, _TCHAR* argv[])
{
MyClass* myClass = new MyClass();
_CrtDumpMemoryLeaks(); //I get a memory leak warning
}
However, if I do:
class MyClass
{
public:
char* NewChar();
};
char* MyClass::NewChar()
{
char* test = new char[100];
return test;
}
MyClass myClass; //Globally declared
int _tmain(int argc, _TCHAR* argv[])
{
char* charPointer = myClass.NewChar();
_CrtDumpMemoryLeaks(); //No warnings
}
Shouldn’t I get the warning since my program exited (right after _CrtDumpMemoryLeaks()) and there was still a new char that wasn’t deleted?
How can I detect these memory leaks?
Also, using the same example above, if I add the code:
char* anotherPointer = charPointer; //previously filled
delete[] anotherPointer;
Will this delete the new char from inside the class preventing memory leaks, or should I call delete on charPointer as well?
If you want to catch (or have a decent shot at catching) global object leaks, try setting the CRT debug flag _CRTDBG_LEAK_CHECK_DF at the start of main. The flag forces a dump of detected leaks after global destructors.
To answer your questions:
Define “warning”. The call to
_CrtDumpMemoryLeaks()should dump whatever is outstanding at the time. Your program exiting afterward will not do another dump unless configured to do so.They should be detected properly provided you are using the debug CRT and have the
_Crtconfiguration setup properly, which it mostly is by default.The following code sets up the
_Crtdump system to dump all objects on-demand and right before final exit (aftermain()is finished and global statics are destroyed).Debug Output Log
Note that the first dump records just the inner allocation, since I used a checkpoint then a dump-since call. The second dump (invoked after
main()is finished) still records them both, as they’re both outstanding.That you’re leak dump after your dynamic allocation (which granted was from an object in global memory space, but that has nothing to do with the allocation, its just code allocating memory and returning it to you) does not look correct. You should be getting an object dump of all outstanding CRT allocations with any
_CrtDumpMemoryLeaks()invoke since the start of the program.