views:

1361

answers:

6

Does anyone know where I can find a memory memory leak detection tool for C++ which can be either run in a command line or as an Eclipse plug-in in Windows and Linux. I would like it to be easy to use. Preferably one that doesn't overwrite new(), delete(), malloc() or free(). Something like GDB if its gonna be in the command line, but I don't remember that being used for detecting memory leaks. If there is a unit testing framework which does this automatically, that would be great.

This question is similar to other questions (such as http://stackoverflow.com/questions/283726/memory-leak-detection-under-windows-for-gnu-c-c ) however I feel it is different because those ask for windows specific solutions or have solutions which I would rather avoid. I feel I am looking for something a bit more specific here. Suggestions don't have to fulfill all requirements, but as many as possible would be nice.

Thanks.

EDIT: Since this has come up, by "overwrite" I mean anything which requires me to #include a library or which otherwise changes how C++ compiles my code, if it does this at run time so that running the code in a different environment won't affect anything that would be great. Also, unfortunately, I don't have a Mac, so any suggestions for that are unhelpful, but thank you for trying. My desktop runs Windows (I have Linux installed but my dual monitors don't work with it) and I'd rather not run Linux in a VM, although that is certainly an option. My laptop runs Linux, so I can use that tool on there, although I would definitely prefer sticking to my desktop as the screen space is excellent for keeping all of the design documentation and requirements in view without having to move too much around on the desktop.

NOTE: While I may try answers, I won't mark one as accepted until I have tried the suggestion and it is satisfactory.

EDIT2: I'm not worried about the cross-platform compatibility of my code, it's a command line application using just the C++ libraries.

+5  A: 

Valgrind is your best friend. Valgrind has a plugin for eclipse. "Sadly" Valgrind does not run on Windows, but it runs on Mac OSX, *BSD and Linux, so I'd consider that "multi-platform". :)

Valgrind does "overwrite" new/delete/malloc/free but not during compilation (so you don't have to recompile if that's what you mean). It interprets the binary so the performance suffer a bit during testing.

LiraNuna
A: 

There's the leaks tool on MacOS X (i don't know if it exists elsewhere) and i used to use a tool called memprof on linux. There's also valgrind which works on linux and is incredibly awesome but has a substantial performance hit, and ostensibly on macos but i haven't tried it.

I am unaware of any such tools on windows however.

olliej
+1  A: 

for 32 bits applications, valgrind + wine can be a working solution as well for windows apps. If your app can be run under wine without any change, then running it under valgrind + wine works well in my (quite limited) experience. Since your app works under Linux, I would guess that your app is likely to run under wine (avoids very windows specific code which is not yet supported in wine).

David Cournapeau
You mean recompiling windows app on *nix using Wine as the api implementation? (i'm just curious)
olliej
valgrind works for x86_64 as well as PowerPC, MIPS, ARM and other architectures.
LiraNuna
No, I mean running your windows binary, unmodified, on linux under wine itself under valgrind. That's why I mentioned 32 bits only: valgrind cannot run apps under wine with 64 bits support, and wine with 64 bits support is experimental (by 64 bits, I mean running 64 bits windows apps)
David Cournapeau
Here's how: http://wiki.winehq.org/Wine_and_Valgrind
LiraNuna
A: 

While you mentioned that this isn't preferred the Boehm Garbage Collector can be used to detect memory leaks. Simply put, if the garbage collector is ever actually running then generally you have a memory leak. It's used this way by Mozilla to detect memory leaks in Firefox.

codeincarnate
+1  A: 

+1 for valgrind. It's brilliant.

Simeon Fitch
A: 

DUMA is a cross-platform leak detection library which I use for many of my projects. It's nice because you don't have to #include any DUMA-specific header, but just link in the library before you link in your system's libc, which contains the memory allocation routines, and after linking in libstdc++.

It can be kind of tricky to set up, especially when used with C++ projects, but I think that it is well worth the time invested. It has helped me to find a few memory leaks before that I might not have discovered otherwise, and one case where I deleted an allocation twice.

One note: it's much easier if you build a static archive (built by default on Windows) because it helps to reduce "false positives" that are actually caused by leaky runtimes.

Daniel Trebbien