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?
Instructions
Things You'll Need
1
Understand the operator basics. The C++ operator "new" allocates heap memory. The "delete" operator frees heap memory. For every "new," you should use a "delete" so that you free the same memory you allocated:
2
Reallocate memory only if you've deleted. In the code below, str acquires a new address with the second allocation. The first address is lost irretrievably, and so are the 30 bytes that it pointed to. Now they're impossible to free, and you have a memory leak:
3
Watch those pointer assignments. Every dynamic variable (allocated memory on the heap) needs to be associated with a pointer. When a dynamic variable becomes disassociated from its pointer(s), it becomes impossible to erase. Again, this results in a memory leak:
4
Be careful with local pointers. A pointer you declare in a function is allocated on the stack, but the dynamic variable it points to is allocated on the heap. If you don't delete it, it will persist after the program exits from the function:
5
Pay attention to the square braces after "delete." Use "delete" by itself to free a single object. Use "delete" [] with square brackets to free a heap array. Don't do something like this:
6
If the leak yet allowed - I'm usually seeking it with deleaker (check it here: http://deleaker.com).
Thanks!
MTuner is a free multi platform memory profiling, leak detection and analysis tool supporting MSVC, GCC and Clang compilers. Features include:
Users can profile any software targeting platforms with GCC or Clang cross compilers. MTuner comes with built in support for Windows, PlayStation 4 and PlayStation 3 platforms.
In addition to the tools and methodes provided in the other anwers, static code analysis tools can be used to detect memory leaks (and other issues as well). A free an robust tool is Cppcheck. But there are a lot of other tools available. Wikipedia has a list of static code analysis tools.
You can use some techniques in your code to detect memory leak. The most common and most easy way to detect is, define a macro say, DEBUG_NEW and use it, along with predefined macros like
__FILE__
and__LINE__
to locate the memory leak in your code. These predefined macros tell you the file and line number of memory leaks.DEBUG_NEW is just a MACRO which is usually defined as:
So that wherever you use
new
, it also can keep track of the file and line number which could be used to locate memory leak in your program.And
__FILE__
,__LINE__
are predefined macros which evaluate to the filename and line number respectively where you use them!Read the following article which explains the technique of using DEBUG_NEW with other interesting macros, very beautifully:
A Cross-Platform Memory Leak Detector
From Wikpedia,
Valgrind http://valgrind.org/
and
GDB http://www.gnu.org/software/gdb/