Jonathan
Jonathan

Reputation: 752

Do memory leaks persist after a C++ program is done executing in Visual Studio 2012?

I'm not new to programming, but new to C++. I find I learn things better when I play and interact with the language. So one cool thing I found was how you can have Visual Studio alert you of any memory leaks in your program via:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h> 

and then call

_CrtDumpMemoryLeaks();

before the program exits and it prints all memory leaks to the output window, awesome!

My questions are

And yes, I know I should really be using smart pointers like shared_ptr, unique_ptr, etc, but I'm doing this on purpose for learning. This isn't a 'real' app, just sandbox tests!

Also, does this way of finding memory leaks, _CrtDumpMemoryLeaks (), have any situations where it doesn't accurately find leaks? It seems like an amazing tool!

Upvotes: 6

Views: 1817

Answers (1)

Chubsdad
Chubsdad

Reputation: 25497

No, The memory leak is in the program. VS or the debugger has nothing to do with the leaked memory once the debuggee has terminated. Unless it is a kernel memory, all user mode memory allocations are freed up by the OS on process termination.

_CrtDumpMemoryLeaks won't be useful in many cases. Once case is leak of any kernel memory (that qualifies as a handle leak in many cases and will require other tools on Windows). You may want to look into WinDbg and the related tools like GFlags (from Debugging Tools for Windows package available from free from Microsoft website) for a more exhaustive diagnostics memory leak/heap corruption etc

Upvotes: 11

Related Questions