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:
Shouldn't I get the warning since my program exited (right after _CrtDumpMemoryLeaks()) and there was still a new char that wasn't deleted?
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.
How can I detect these memory leaks?
They should be detected properly provided you are using the debug CRT and have the _Crt
configuration setup properly, which it mostly is by default.
The following code sets up the _Crt
dump system to dump all objects on-demand and right before final exit (after main()
is finished and global statics are destroyed).
class MyLeak
{
public:
MyLeak() { new unsigned char[1024]; }
char * NewChar() { return new char[1024]; }
};
MyLeak myLeak;
int main(int argc, char *argv[])
{
int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(tmpFlag);
_CrtMemState ms = {0};
_CrtMemCheckpoint(&ms);
char *ptr = myLeak.NewChar();
_CrtMemDumpAllObjectsSince(&ms);
OutputDebugString("Exiting main()\n");
return EXIT_SUCCESS;
}
Debug Output Log
Dumping objects ->
{69} normal block at 0x000000000048B800, 1024 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
Exiting main()
Detected memory leaks!
Dumping objects ->
{69} normal block at 0x000000000048B800, 1024 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
{68} normal block at 0x000000000048B390, 1024 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
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.