How to check for memory leaks in C++

Started by
12 comments, last by MARS_999 20 years, 7 months ago
What you are saying seems like it would work. It seems to work on the most primitive case:
#include <crtdbg.h>#include <windows.h>int WinMain( HINSTANCE, HINSTANCE, LPSTR, int ){    _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF);     char* pMemory = new char[1000];    return 0;}Behold:   Detected memory leaks!Dumping objects ->{61} normal block at 0x002F3978, 1000 bytes long. Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete.The program '[1104] MemCheckWin.exe: Native' has exited with code 0 (0x0).




[edited by - mauman on September 3, 2003 2:57:14 PM]
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Advertisement
quote:Original post by MauMan
What you are saying seems like it would work. It seems to work on the most primitive case:
#include <crtdbg.h>#include <windows.h>int WinMain( HINSTANCE, HINSTANCE, LPSTR, int ){    _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF);     char* pMemory = new char[1000];    return 0;}Behold:   Detected memory leaks!Dumping objects ->{61} normal block at 0x002F3978, 1000 bytes long. Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete.The program '[1104] MemCheckWin.exe: Native' has exited with code 0 (0x0).




[edited by - mauman on September 3, 2003 2:57:14 PM]


Ok but do I need to include that function in ever function where I call new and delete? I am calling new and delete in my classes. usually delete in the deconstructor and new in the constructor or someother function within the class. Thanks
update:
I just compiled your example and this is what I get in the debug window at the bottom of VC++6
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.

So no I am not seeing the Deteced memory leaks???

[edited by - Mars_999 on September 3, 2003 3:42:03 PM]
quote:Original post by MARS_999
Ok but do I need to include that function in ever function where I call new and delete? I am calling new and delete in my classes. usually delete in the deconstructor and new in the constructor or someother function within the class. Thanks


My solution:

#define _CRTDBG_MAP_ALLOC#include <crtdbg.h>struct MemLeakTest{	MemLeakTest() 	{	}		~MemLeakTest() 	{		OutputDebugString("Beginning dump of memory LEAKS:\n");		_CrtDumpMemoryLeaks(); 		OutputDebugString("Any and all memory LEAKS dumped.\n");	}};


Insure this is the last object destructed in your code. It depends on your compiler; one way to insure it is to print a message for each global object you have destructed and tweak until this class is the last one destructed. Anyhow, after you've managed to force this to be the class destructed, all memory leaks will be dumped after the program terminates.

EDIT: And in case you were not aware, global objects are the last objects to have their destructor called. So, needless to say, this class will have to be a global object.

[edited by - haro on September 3, 2003 3:46:30 PM]
The best one I''ve seen so far is Fluid Studios'' MMGR
daerid@gmail.com

This topic is closed to new replies.

Advertisement