Archived

This topic is now archived and is closed to further replies.

MARS_999

How to check for memory leaks in C++

Recommended Posts

MARS_999    1627
I have just finished using some dynamic allocation of memory that I am usure of if its correctly deleted? I know the memory is allocated right because my images are displayed correctly. So is their an app that I can run that will tell me I have a memory leak. Thanks

Share this post


Link to post
Share on other sites
cynicalpenguin    122
Well, you could try including crtdbg.h and then calling _CrtSetDbgFlag( _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF) at the beginning of the program then run the program in debug mode. It will output to the debug screen when the program ends if you''ve left anything behind. I''m really not sure how reliable it is though

Share this post


Link to post
Share on other sites
crazyjul    122
You can overload the new and delete operation to keep track a memory allocated. If you want to see a real good memory manager, the Nebula''s one (www.radonlabs.de) is very impressive. If you want something simpler, I can give you mine just contact me

julien.hamaide@multitel.be

Share this post


Link to post
Share on other sites
The_Fallen    122
Paul''s memory manager is great, but unfortunately it has some problems when using STL, so you should not use it, when using some containers from the standard library...
@crazyjul: Where on the radlonlabs page is it? I can''t find any source downloads... Or maybe I''m just blind...

Share this post


Link to post
Share on other sites
MARS_999    1627
Regarding Pauls memory checker. I got an error. Well when I trace through with debugger I use new in a function and call delete in the deconstructor I get an error using his code? So what gives he wants you to call new/delete in the same block of code?? Thanks

Share this post


Link to post
Share on other sites
MARS_999    1627
quote:
Original post by antareus
I use:
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF);   


Always worked fine for me.


When I used it I got no reports even if I intentionally left out a delete. I never got any error or statement saying otherwise?

Maybe I am using it wrong? All I should have to do is call _CrtSetDbgFlag() at the beginning of WinMain() right? I am using new and delete, or is this function for malloc and free only? Thanks

[edited by - Mars_999 on September 3, 2003 2:42:40 PM]

Share this post


Link to post
Share on other sites
MauMan    344
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]

Share this post


Link to post
Share on other sites
MARS_999    1627
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]

Share this post


Link to post
Share on other sites
haro    502
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]

Share this post


Link to post
Share on other sites