How to check for memory leaks in C++

Started by
12 comments, last by MARS_999 20 years, 7 months ago
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
Advertisement
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
It's time for a revolution I say! A revolution!
Thanks but nope it didn''t work. I intentionally left out a delete and it came back with nothing.
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
you may try this, it works well when properly inserted in your project (the compiler sometimes complains)

Paul Nettle Memory Manager

[edited by - bucheron on September 3, 2003 5:12:35 AM]
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...
I don''t use STL. So Pauls app should work fine for me then?
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
I use:
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF); 


Always worked fine for me.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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]

This topic is closed to new replies.

Advertisement