Jump to content
  • Advertisement
Sign in to follow this  
yanuart

help me find my mem leaks

This topic is 4726 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, i've tried these methods to trap my memory leaks 1. _CrtDumpMemoryLeaks(); the result are just object dump without a clue where in my code cause the leak, I've tried #define CRTDBG_MAP_ALLOC, but that doesn't help either. 2. #define new DEBUG_NEW .. when I compile this, it shows error saying that DEBUG_NEW is unknown, I've look in msdn and it seems it'll ony work if you create an MFC program, is that right ? which one do you guys use ? and any help on what did I do wrong ?

Share this post


Link to post
Share on other sites
Advertisement

#include <crtdbg.h>
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag
flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag(flag); // Set flag to the new value

//and then:
#define new new(_NORMAL_BLOCK,__FILE__, __LINE__)


all done!
the new redefine has to be done before any of the code you are trying to check for leaks.

more info:
Memory Debugging with Microsoft VC

Share this post


Link to post
Share on other sites
Keep in mind that it might look like there are memory leaks, but it could just be static data that hasn't been released yet. So I wouldn't go tracking down every 4byte memory leak that _CrtDumpMemoryLeaks() shows you

Share this post


Link to post
Share on other sites
This is what I use:

#include <crtdbg.h>

class A
{
public:
int *p;
A() { p = new int; }
// ~A() { delete p; }
};

static A a;

int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
return 0;
}

Share this post


Link to post
Share on other sites

// ...before any includes...
//#define USE_MEMLEAK_DETECTION
#ifdef USE_MEMLEAK_DETECTION
#define CRTDBG_MAP_ALLOC
#include "crtdbg.h"
#endif


//...at the beginning of main()...
#ifdef USE_MEMLEAK_DETECTION
int tmp = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );// get the current bits
tmp &= 0x0000FFFF; // clear the upper 16 bits
tmp |= 0x00010000; // and OR in the desired freqency
tmp |= _CRTDBG_CHECK_ALWAYS_DF; // all checks
tmp |= _CRTDBG_LEAK_CHECK_DF; // dump mem leaks
_CrtSetDbgFlag( tmp ); // set the new bits
// _CrtSetBreakAlloc( 4840 ); // break at allocation
#endif


Share this post


Link to post
Share on other sites
hmm.. still dumping like this :
5220} normal block at 0x00F8EE28, 140 bytes long.
Data: <2 TC C ? > 32 AA 54 43 D1 84 BA 43 00 00 00 3F FF FF FF FF

I still couldn't pinpoint which of my code cause the leak,
#define new new(_NORMAL_BLOCK,__FILE__, __LINE__) cause error to compile.
Some syntax errors mostly, I've looked on MSDN and it doesnt got a hint on this.

can someone help me ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!