Leaks with CRT

I'm a big fan of the Windows instrumented debug heap, but today I want to talk about the CRT support, which can sometimes provide a bit more context.

You can make a build-time decision to use the CRT debug heap. A nice thing about these is that the function calls to the debug heap disappear when _DEBUG is not defined, which is presumably how your release build works.

This adds a debug allocator on top of the release allocator, and enables a bunch of new functionality. The most useful ones are these:

Here's how you would put the above functions together.

_CrtMemState startState{};
_CrtMemCheckpoint(&startState);

// Allocate things and presumably deallocate them again!

// Set the memory dump to print to `stderr` and write objects we've allocated and not freed since starting.
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
_CrtMemDumpAllObjectsSince(&startState);

For the official doc and step-by-step instructions, see Find memory leaks with the CRT library.

Happy leak tracking!

Tags:  cppdebugging

Home