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
- Do I need to restart Visual Studio 2012 after I find a memory leak?
- Do I need to restart my computer?
- How long do these things stay leaked? Or does Windows clean up all memory at the end of execution? If that’s true, does it stay true for running a program in “debug mode” in Visual Studio?
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!
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