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!
Modern operating system recollects lost memory after program termination. Potentially dangerous. These applications continue to waste memory over time, eventually consuming all RAM resources.
The Visual Studio debugger and C Run-time Library (CRT) can help you detect and identify memory leaks.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With