Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C++

Memory Leak Finder

Rate me:
Please Sign up or sign in to vote.
3.68/5 (39 votes)
22 Feb 2012MIT2 min read 345.3K   4.5K   126  
Custom memory handler with memory leak reporting and no-mans-land checking. Leaks are reported with call stack of allocation.
//#if defined(_DEBUG) && defined(WIN32) && defined(DETECT_LEAKS)
#if defined(WIN32) && defined(DETECT_LEAKS)

#include "MemLeakFindDll.h"

// Take over global new and delete
void* operator new(size_t s)
{
  return TraceAlloc(s);
}

void* operator new[](size_t s)
{
  return TraceAlloc(s);
}

void operator delete(void* pMem)
{
  TraceDealloc(pMem);
}

void operator delete[] (void* pMem)
{
  TraceDealloc(pMem);
}

// And then some crap for taking over MFC allocations.
void* __cdecl operator new(size_t s, LPCSTR lpszFileName, int nLine)
{
  return TraceAlloc(s);
}

void* __cdecl operator new[](size_t s, LPCSTR lpszFileName, int nLine)
{
  return TraceAlloc(s);
}

void __cdecl operator delete(void* pMem, LPCSTR /* lpszFileName */, int /* nLine */)
{
  TraceDealloc(pMem);
}

void __cdecl operator delete[](void* pMem, LPCSTR /* lpszFileName */, int /* nLine */)
{
  TraceDealloc(pMem);
}

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior)
Sweden Sweden
B.Sc in Software engineering

Writing software for the finance market.
Languages known: C/C++, SQL, Java, Perl, M68000 assembly and more. Give me the syntax and I'll program in it.

In my spare time i like to watch movies, read books and play computergames.

Comments and Discussions