How to detect memory leaks???(c++)

Started by
15 comments, last by johnnyBravo 19 years, 10 months ago
Hi, ive just wrapped up directsound8 classes(c++), and im because of the large amount of code im afraid i may of left memory leaks. Im wondering how would i test if there is any leaks going on? Thanks, [edited by - johnnyBravo on May 27, 2004 8:55:41 PM]
Advertisement
There are a couple of ways to deal with this:

1. Customize your memory handling routines and verify that all your resources are being released correctly. I think the Game Gems series has a few articles on this.

2. Bounds-checker.

3. Purify.

I personally like purify but this is not an inexpensive tool. #1 may be your best bet for a low budget solution. You could also incorporate some advanced memory handling techniques to deal with the fragmentation issues that are prevalent in handling the thousands of resources you need to keep resident.


#dth-0
"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst
I got some pretty good replies to my thread when I asked the same thing... Check that out. http://www.gamedev.net/community/forums/topic.asp?topic_id=199376

-Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Use a profiler. I just started using one called glowcode which works pretty well.
#if defined (_DEBUG)
CMemoryState msOld,msNew,msDiff;
#endif

void main(){

#if defined (_DEBUG)
msOld.Checkpoint();
#endif

//program code here

#if defined (_DEBUG)
msNew.Checkpoint();
if(msDiff.Difference(msOld,msNew)){
TRACE("Memory leaked!\n");
msDiff.DumpStatistics();
}
#endif

}

[edited by - wavarian on May 27, 2004 12:14:32 AM]
Yay!
thanks all, i ended up using alnight''s
#define CRTDBG_MAP_ALLOC#include <stdlib.h>#include <crtdbg.h>//_CrtDumpMemoryLeaks();


and to my amazement, after remembering to put in the delete on the pointers, works perfectly, no memory leaks from my wrapper, which i dont know how its possible

Personally, I overload the new and delete operators, have new increment a counter everytime memory is allocated, have delete decrement a counter when memory is deallocated, and then check that my counter == 0 at the end of the program. This seems to be more cross-platform than using Microsoft''s debugging stuff, if cross-platform is something that you''re looking for...
don''t know if this will work. its from the gamedev newsletter:

===================5. Tip of the Week-------------------Simple Memory Leak Detection in Visual StudioBy: Drew "Gaiiden" Sikora=============================================I stumbled across this in my wanderings last night. All credit, of course,goes out to wheoever first came up with this, which wasn''t me. Accordingto Bad Maniac over at http://2dnow.zenzer.net/index.php, it was brought tohis attention by Ethereal Darkness of http://etherealdarkness.com/ and nowI bring it to you, because it rocks.#include "crtdbg.h"void DetectMemoryLeaks(){   _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);   _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);   _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);}Now just call this function at the start of your program and if you''recompiling in debug mode (F5), any leaks will be displayed in the Outputwindow when the program shuts down. If you''re not in debug mode this willbe ignored. Use it as you will!
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
DirectX isn''t that cross platform thing. So the usage of _CrtDumpMemoryLeaks is the easiest solution in this case.

Toolmaker


My site
/* -Earth is 98% full. Please delete anybody you can.*/

This topic is closed to new replies.

Advertisement