help me find my mem leaks
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 ?
#include <crtdbg.h>int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flagflag |= _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
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
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;}
// ...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
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 ?
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 ?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement