6

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.

Vijay
  • 2,021
  • 4
  • 24
  • 33

6 Answers6

5

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    +1 from me. This is one of my favorite ways to detect leaks but maintaining a list of allocated addresses and checking them against address being sent for deletion on every delete is performance incentive(O(n)=nlog(n)).It is important to mention here, to use the technique only in debug builds and not on release builds. – Alok Save Apr 04 '11 at 05:52
  • @Als: I thought it was obvious that this technique should be used in debug build only, as the macro name I chose (or usually chosen) is `DEBUG_NEW`. – Nawaz Apr 04 '11 at 05:54
  • Just a explicit mention of the same. You have my +1 anyways :) I couldn't see any unicorn just as other day though.... – Alok Save Apr 04 '11 at 05:57
4

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

ConfusedSushi
  • 874
  • 8
  • 16
2

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).

Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
2

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 :)

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
0

If you're using VC++, that functionality is built in. See Finding Memory Leaks Using the CRT Library for non-MFC apps and Detecting memory leaks in MFC for MFC apps.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
0

Addition to the above, I can advise a few good programs :^) As example? it would be good to use deleaker for Windows.

MastAvalons
  • 1,150
  • 1
  • 14
  • 23