27

What is the best tool to detect memory leaks.

Both free and not Free.

Thanks

Bruce McGee
  • 15,076
  • 6
  • 55
  • 70
Jlouro
  • 4,515
  • 9
  • 60
  • 91

9 Answers9

40

FastMM is a free (source available) memory leak detector, already integrated in the latest Delphi versions. I never had a need for anything else.

It's much better BTW to limit the possibilities of memory leaks when coding, instead of finding them later. Some tips:

Always use try and finally in your code to free created objects. Better to write this code immediately, and then write code to use the objects. Even better to make use of IDE functionality like Code Templates.

Only use functions that return dynamically allocated objects when absolutely necessary. It's generally better to pass objects as parameters than create and return them. For example this

procedure getChoices(var AChoices: TStrings);

would be much better than

function getChoices: TStrings;

as there is no potential of accidently leaking the created TStrings object.

mghie
  • 32,028
  • 6
  • 87
  • 129
  • 8
    You can turn it on by setting ReportMemoryLeaksOnShutdown := True; Or get the latest version from sourceforge and set the right options in the .inc file. – Lars Truijens Jan 06 '09 at 11:06
  • with getChoices do try .. finally free; end; – inzKulozik Jan 06 '09 at 18:33
  • 1
    @inzKulozik: Yes, it does not need to cause a leak, but the potential is there. OTOH the first version might work without ever creating another TStrings object. You could for example pass the Items of a TListBox or TMemo. – mghie Jan 06 '09 at 20:58
  • 5
    Better still: procedure getChoices(const AChoices: TStrings); You can still modify properties of AChoices, but not assign it to another object. – Gerry Coll Jan 07 '09 at 02:54
  • @LarsTruijens I never knew about ReportMemoryLeaksOnShutdown thanks :) –  Dec 03 '11 at 19:58
  • There is no need for the `var` on `AChoices`. It is already a reference type. – dan-gph Nov 30 '16 at 14:46
20

We use EurekaLog at our work in Delphi 7. It's an exception handler component which gives very detailed information about exceptions (including callstack! environment variables, etc) even for access violations. But another great feature is that you can tell it to error on memory leaks too, which shows the exact line of code where the memory/object was allocated in the first place! It is a commerical product but I would still highly recommend it.

CodeAndCats
  • 7,508
  • 9
  • 42
  • 60
  • 1
    Just don't forget that EL is exception-tracer in the first place ;) So its memory-leaks features is limited, compared to features of, say, FastMM (see answer of mghie). – Alex Apr 25 '09 at 11:39
  • I was considering getting EurekaLog over MadExcept because of the memory leaks reporting. But unfortunately it cannot catch memory leaks in projects compiled with Run-Time Packages. So for me, I couldn't use it. – lkessler Aug 10 '09 at 16:04
  • Update: I just purchased EurekaLog. I managed to get my product to compile without the run time packages. It's great. – lkessler Nov 22 '09 at 02:18
14

AQTime is very good. It also does other things like profiling for performance. And it does not require any changes in your code. Of course compiling with debug info helps giving better results.

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • Agree with AQTime, even for Delphi, but especially for everything Visual Studio. Worth every single cent. – OregonGhost Jan 06 '09 at 11:20
  • 2
    The good of AQTime it is it works with many compilers. It could be more difficult to use, and unlike FastMM it can't be installd everywhere and requires code instrumentation. But it can help to pinpoint leaks exactly. I usually start with FastMM and then drill down with AQTime. –  Mar 16 '10 at 17:23
14

I would recommend the full FastMM4-version over the version included in Delphi. The full version gives you a full and verbose report logged to file instead of the dialogbox. Combine this with a homebrewed 'memoryleak-logfile-differ' your're all set for an exiting bughunt.

Vegar
  • 12,828
  • 16
  • 85
  • 151
  • 4
    And you can use the FastMMUsageTracker.pas file which creates a separate window showing how memory is used updated every second or so – mjn Nov 10 '09 at 11:06
10

I always use ReportMemoryLeaksOnShutdown := (DebugHook <> 0); so that I only get the leaks reported if I'm debugging.

ps wanted to place this in a comment but I dont have the rights yet..

Michiel T
  • 537
  • 9
  • 23
7

You may want to give a look at this CodeRage 2 session: Fighting Memory Leaks for Dummies. It mainly shows how to use FastMM to prevent/detect memory leaks in Delphi. Was for D2007 but still relevant for D2009.

Francesca
  • 21,452
  • 4
  • 49
  • 90
7

FastMM is very good. I know 3 or 4 free Memory managers for Delphi. It takes less than hour to check all them, cause usually they require just 3 lines of code to embed'em in project.

4

I use MadExcept because it's free for personal use.

rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62
1

Try UMLD 1.0 We have tried and tested!! Smart Solution to a Leakfree Delphi Application Development and Bug Reporting Tool for Delphi development.

http://www.torry.net/authorsmore.php?id=7616&sort=ID