Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • 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!

like image 366
Jonathan Avatar asked Jan 28 '13 02:01

Jonathan


People also ask

Do memory leaks persist after program ends?

Modern operating system recollects lost memory after program termination. Potentially dangerous. These applications continue to waste memory over time, eventually consuming all RAM resources.

Does Visual Studio detect memory leaks?

The Visual Studio debugger and C Run-time Library (CRT) can help you detect and identify memory leaks.

Is memory leak permanent?

Physical or permanent damage does not happen from memory leaks. Memory leaks are strictly a software issue, causing performance to slow down among applications within a given system. It should be noted a program taking up a lot of RAM space is not an indicator that memory is leaking.

What happens when memory leak in C?

A memory leak in C typically happens when the pointer loses its initial allocated address due to which the pointed memory block cannot be accessed or cannot de-allocated, and this becomes the reason for the memory leaks.


1 Answers

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

like image 94
Chubsdad Avatar answered Nov 14 '22 22:11

Chubsdad