WWW.XYZW.DE The homepage of Dierk "Chaos" Ohlerich  
Home Releases Codes
Types FPU intrinsics Input Lag Iterate & Delete Load & Save Memory Leaks About Exceptions AGP Writes

Visual C++ has some good features for finding memory leaks. But when you are not using MFC, the compiler will not tell you the line where you allocated the memory.

Here is a hack that add's that info to Visual C++ memory checking. Put this in your most important header:

/****************************************************************************/

#define _MFC_OVERRIDES_NEW
void * __cdecl operator new(unsigned int,const char *,int);
inline void __cdecl operator delete(void *p, const char *, int) 
{ ::operator delete(p); }
#define new new(__FILE__,__LINE__)

/****************************************************************************/

The implementation:

/****************************************************************************/

// this is the hack

#undef new
void * __cdecl operator new(unsigned int size,const char *file,int line)
{
  return _malloc_dbg(size,_NORMAL_BLOCK,file,line);
}
#define new new(__FILE__,__LINE__)

// initialize memory debugging
// if you don't see the testleak, then you know it's not working!

#include <crtdbg.h>

void MemInit()
{
  _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)|
    _CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
  sChar *test = new sChar[16];
  sCopyString(test,"TestLeak",16);
}

// nothing needed when exiting

void MemExit()
{
}

// when you abort, you don't need to check for memory leaks
// most likely you will have thousands....

void MemAbort()
{
  _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)&
    ~(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF));
}

/****************************************************************************/

If you need d3dx9.h, you might run into some trouble. Do the following:

/****************************************************************************/

#include "types.hpp"      // my own headers are incompatible with windows.h
#include "intro.hpp"      // they must come first  

#include <windows.h>      // now I include windows
#include <d3d9.h>         // and d3d9
#undef new
#include <d3d9x.h>        // but d3d9x needs unmodified operator new
#define new new(__FILE__,__LINE__)

/****************************************************************************/