Jump to content
  • Advertisement

Archived

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

johnnyBravo

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

This topic is 5260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites
don''t know if this will work. its from the gamedev newsletter:


===================
5. Tip of the Week
-------------------
Simple Memory Leak Detection in Visual Studio
By: 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. According
to Bad Maniac over at http://2dnow.zenzer.net/index.php, it was brought to

his attention by Ethereal Darkness of http://etherealdarkness.com/ and now

I 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''re
compiling in debug mode (F5), any leaks will be displayed in the Output
window when the program shuts down. If you''re not in debug mode this will
be ignored. Use it as you will!

Share this post


Link to post
Share on other sites
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.*/

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!