Skip to content

Instantly share code, notes, and snippets.

@qpfiffer
Created October 13, 2011 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save qpfiffer/1284439 to your computer and use it in GitHub Desktop.
Save qpfiffer/1284439 to your computer and use it in GitHub Desktop.
C++ Memory Leak Detection in VS2010
#ifdef _DEBUG
#include <ostream>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
// ...
#ifdef _WIN32
_CrtDumpMemoryLeaks();
#endif
// Update 10/18/2011. Better to use the following for multiple exit points:
// add this code in main
#ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
// Breaking on a certain allocation:
_CrtSetBreakAlloc(137); // Will break on the 137th alloc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment