Programming Tips & Tricks

Home Categories: C# | C++ | General | Other

C++ Tips & Tricks: Discovering Memory Leaks

If you are working on a serious project, you will propably use a sophisticated memory manager which will also discover memory leaks (along with their exact position in the code), but if you just want to do a quick test in a small program, there is an easy way to let the debugger check for memory leaks:
- Include crtdbg.h in your project (this file is included in the Microsoft Platform SDK)
- At the beginning of main() or WinMain () put this code:
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
This works in Visual C++ ( i don't know about other compilers though ) and, if you run the programm in debug mode, it will report memory leaks in the debug output window ("Immediate Window" in the latest Visual Studio version).