Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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