views:

1383

answers:

14

What memory leak detectors have people had a good experience with?

Here is a summary of the answers so far:

Valgrind - Instrumentation framework for building dynamic analysis tools.

Electric Fence - A tool that works with GDB

Splint - Annotation-Assisted Lightweight Static Checking

Glow Code - This is a complete real-time performance and memory profiler for Windows and .NET programmers who develop applications with C++, C#, or any .NET Framework

Also see this stackoverflow post.

+11  A: 

Valgrind under linux is fairly good; I have no experience under Windows with this.

hazzen
+1  A: 

I've had minimal love for any memory leak detectors. Typically there are far too many false positives for them to be of any use. I would recommend these two as beiong the least intrusive:

GlowCode

Debug heap

1800 INFORMATION
+2  A: 

lint (very similar open-source tool called splint)

Ben Collins
+12  A: 

second the valgrind... and I'll add electric fence.

nlucaroni
+1 electric fence. Seems quite useful.
Tom
A: 

Some very good tips for C at How do you detect/avoid memory leaks

Prakash
+2  A: 

Painful but if you had to use one..
I'd recommend the DevPartner BoundsChecker suite.. that's what people at my workplace use for this purpose. Paid n proprietary.. not freeware.

Gishu
I've used BoundsChecker myself. It's been *incredibly* helpful, both for memory leaks and other kinds of resource leaks.
Herms
A: 

At university when I was doing most things under Unix Solaris I used gdb.

However I would go with valgrind under Linux.

Marcel Tjandraatmadja
A: 

I'll second the valgrind as an external tool for memory leaks.
But, for most of the problems I've had to solve I've always used internally built tools. Sometimes the external tools have too much overhead or are too complicated to set up.

Why use already written code when you can write your own :)

I joke, but sometimes you need something simple and it's faster to write it yourself. Usually I just replace calls to malloc() and free() with functions that keep better track of who allocates what. Most of my problems seem to be someone forgot to free and this helps to solve that problem.

It really depends on where the leak is, and if you knew that, then you would not need any tools. But if you have some insight into where you think it's leaking, then put in your own instrumentation and see if it helps you.

Alan H
+1  A: 

For Win32 debugging of memory leaks I have had very good experiences with the plain old CRT Debug Heap, that comes as a lib with Visual C.

In a Debug build malloc (et al) get redefined as _malloc_dbg (et al) and there are other calls to retrieve results, which are all undefined if _DEBUG is not set. It sets up all sorts of boundary guards on the heap, and allows you to diplay the results at any time.

I had a few false positives when I was witting some time routines that messed with the library run time allocations until I discovered _CRT_BLOCK.

I had to produce first DOS, then Win32 console and services that would run for ever. As far as I know there are no memory leaks, and in at least one place the code run for two years unattended before the monitor on the PC failed (though the PC was fine!).

David L Morris
A: 

The grandaddy of these tools is the commercial, closed-source Purify tool, nowadays part of IBM's Rational suite.

Parasoft's Insure++ (source code instrumentation) and valgrind (open source) are the two other real competitors.

Trivia: the original author of Purify, Reed Hastings, went on to found NetFlix.

djsadinoff
+3  A: 

If you have the money: IBM Rational Purify is an extremely powerful industry-strength memory leak and memory corruption detector for C/C++. Exists for Windows, Solaris and Linux. If you're linux-only and want a cheap solution, go for Valgrind.

Thorsten79
What does Purify does that Valgrind doesn't? Last time I've tried Purify, it was a real pain to set up and get anything out of it, plus you had to compile using their compiler. Valgrind uses your normal debug build.
florin
+3  A: 

Mudflap for gcc! It actually compiles the checks into the executable. Just add

-fmudflap -lmudflap

to your gcc flags.

Artelius
+1  A: 

On Windows, I have used Visual Leak Detector. Integrates with VC++, easy to use (just include a header and set LIB to find the lib), open source, free to use FTW.

Chris Morley
+1  A: 

Also worth using if you're on Linux using glibc is the built-in debug heap code. To use it, link with -lmcheck or define (and export) the MALLOC_CHECK_ environment variable with the value 1, 2, or 3. The glibc manual provides more information.

This mode is most useful for detecting double-frees, and it often finds writes outside the allocated memory area when doing a free. I don't think it reports leaked memory.

Ben Combee