Travis Banger
Travis Banger

Reputation: 717

Windows Development: How to determine whether my app is leaking memory?

A long time ago I was told about some statement that you add at the beginning of the application and when it is done, the facility informs whether the app has unreclaimed memory.

TIA

Addition

Here it is:

http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80%29.aspx

Upvotes: 1

Views: 48

Answers (1)

Adrian McCarthy
Adrian McCarthy

Reputation: 48038

The debug C run-time library with Visual Studio can track all allocations and automatically report any that aren't freed at application exit. First, include <crtdbg.h>, and then at the very beginning of your program, ask it to track allocation and report leaks by making this call:

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

In the debug output window of the Visual Studio debugger (or another program that monitors the debug output), you'll see a report of leaked allocations when the application ends.

In general, you probably only want to do this in a debug build, as there is a nontrivial performance impact.

Also note that if you allocate singletons and never free them, they will (not surprisingly) be reported as leaks.

Full details are in MSDN.

Upvotes: 1

Related Questions