Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ memory leak auto detection library [closed]

Tags:

c++

memory

I am searching for memory leak detection library. Something like I would just include it into source code then it should start detecting. External programs might be good but i was looking for some library which can be linked into executable.
This i am searching for Windows.

like image 911
Vijay Avatar asked Apr 03 '11 16:04

Vijay


4 Answers

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:

#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW

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,

Debug_new refers to a technique in C++ to overload and/or redefine operator new and operator delete in order to intercept the memory allocation and deallocation calls, and thus debug a program for memory usage. It often involves defining a macro named DEBUG_NEW, and makes new become something like new(_FILE_, _LINE_) to record the file/line information on allocation. Microsoft Visual C++ uses this technique in its Microsoft Foundation Classes. There are some ways to extend this method to avoid using macro redefinition while still able to display the file/line information on some platforms. There are many inherent limitations to this method. It applies only to C++, and cannot catch memory leaks by C functions like malloc. However, it can be very simple to use and also very fast, when compared to some more complete memory debugger solutions.

like image 113
Nawaz Avatar answered Oct 26 '22 06:10

Nawaz


I can suggest Visual Leak Detector, it is much easier to use than the Visual Studio built-in one.

like image 20
ConfusedSushi Avatar answered Oct 26 '22 04:10

ConfusedSushi


For me this was for very long time the best tool: http://www.paulnettle.com/pub/FluidStudios/MemoryManagers/Fluid_Studios_Memory_Manager.zip Just include 1 header file and you're done with it :)

like image 37
Ferenc Deak Avatar answered Oct 26 '22 04:10

Ferenc Deak


Visual studio has such feature on Windows. See http://msdn.microsoft.com/en-us/library/e5ewb1h3(v=VS.90).aspx . Under linux I do not know if such things exist, but valgrind is really good to find all kind of memory problems (not only leaks, but also invalid reads for instance).

like image 43
Janick Bernet Avatar answered Oct 26 '22 06:10

Janick Bernet