I am a C++ programmer on the Windows platform. I am using Visual Studio 2008.
I usually end up in the code with memory leaks.
Normally I find the memory leak by inspecting the code, but it is cumbersome and is not always a good approach.
Since I can't afford a paid memory leak detection tool, I wanted you guys to suggest the best possible ways to avoid memory leaks.
- I want to the know how the programmer can find memory leaks.
- Is there any standard or procedure one should follow to ensure there is no memory leak in the program?
You can use the tool Valgrind to detect memory leaks.
Also, to find the leak in a particular function, use exit(0) at the end of the function and then run it with Valgrind
Visual Leak Detector (VLD) is a free, robust, open-source memory leak detection system for Visual C++.
If you only have crash dumps, you can use the Windbg
!heap -l
command, it will detect leaked heap blocks. Better open the gflags option: “Create user mode stack trace database”, then you will see the memory allocation call stack.There are some well-known programming techniques that will help you to minimize the risk of getting memory leaks at first hand:
new
anddelete
always pairwise, and make sure the allocation/deallocation code is called pairwisevector<T> t
whereever possible instead ofT* t = new T[size]
AddressSanitizer (ASan) is a fast memory error detector. It finds use-after-free and {heap,stack,global}-buffer overflow bugs in C/C++ programs. It finds:
This tool is very fast. The average slowdown of the instrumented program is ~2x.
gflags
utility to turn on user-mode stack traces.UMDH
to take multiple snapshots of your program's memory. Take a snapshot before memory gets allocated, and take a second snapshot after a point at which you believe that your program has leaked memory. You might want to add pauses or prompts in your program to give you a chance to runUMDH
and take the snapshots.UMDH
again, this time in its mode that does a diff between the two snapshots. It will then generate a report containing the call stacks of suspected memory leaks.gflags
settings when you're done.UMDH
will give you more information than the CRT debug heap because it is watching memory allocations across your entire process; it can even tell you if third-party components are leaking.Search your code for occurrences of
new
, and make sure that they all occur within a constructor with a matching delete in a destructor. Make sure that this is the only possibly throwing operation in that constructor. A simple way to do this is to wrap all pointers instd::auto_ptr
, orboost::scoped_ptr
(depending on whether or not you need move semantics). For all future code just ensure that every resource is owned by an object that cleans up the resource in its destructor. If you need move semantics then you can upgrade to a compiler that supports r-value references (VS2010 does I believe) and create move constructors. If you don't want to do that then you can use a variety of tricky techniques involving conscientious usage of swap, or try the Boost.Move library.