How to detect memory leaks in Free Pascal/Lazarus?
Asked Answered
L

2

12

In Delphi, I usually write a simple leak test like this:

program MemLeak;

{$APPTYPE CONSOLE}

uses
    SysUtils;

procedure Leak;
begin
    { Put leaking code here. }
end;

begin
    ReportMemoryLeaksOnShutdown:= True;
    try
        Leak;
    except
        on E: Exception do
            Writeln(E.ClassName, ': ', E.Message);
    end;
end.

How do I detect memory leaks in Free Pascal/Lazarus?

Lati answered 14/10, 2012 at 4:20 Comment(1)
There is an ongoing effort to port FastMM4 into FPC. Perhaps you could be a part. Then you could run that your code unmodified if FastMM4/FPC would be stable. lists.freepascal.org/lists/fpc-pascal/2005-December/009721.html lists.freepascal.org/lists/fpc-pascal/2009-June/021448.htmlInfante
W
18

Free Pascal has a similar feature. At the end of the program, call DumpHeap, or enable the heaptrc option in the Lazarus project settings. The output file can be set with the SetHeapTraceOutput method. Both methods are in the unit heaptrc which must be the first in the project (to capture allocations from start).

More information:

Leak visualization: the Lazarus package "LeakView" presents the content of a heap trace output file in a tree view. It is included in the default installation and available after a rebuild of the IDE. (not yet tested by me)

  // By default information is written to standard output, 
  // this function allows you to redirect the information to a file
  SetHeapTraceOutput('heaptrace.log');

  // normally the heap dump will be written automatically at the end,
  // but can also be written on demand any time   
  DumpHeap;

The output looks like:

C:\path\to\Demo.exe 
Heap dump by heaptrc unit
244 memory blocks allocated : 8305/9080
241 memory blocks freed     : 8237/9000
3 unfreed memory blocks : 68
True heap size : 458752
True free heap : 458288
Should be : 458480
Call trace for block $0010CE58 size 28
  $0044ACCB  TIDTHREADSAFE__CREATE,  line 226 of C:/path/to/indy-10.5.8.tiburon/Lib/Core/IdThreadSafe.pas
  $00444245  IDTHREAD_init,  line 641 of C:/path/to/indy-10.5.8.tiburon/Lib/Core/IdThread.pas
  $00409D74
  $0040E1A1
  ...

(tested with Free Pascal 2.6.0)

Whinchat answered 14/10, 2012 at 5:51 Comment(1)
In a gui Lazarus project, when do you put SetHeapTraceOutput('heaptrace.log');. in the main formclose or oncreate eventMousetail
S
6

While mjn is completely right, and what he says is the prefered solution, on *nix one can also use unit "cmem" (first unit in main program) to switch the memory manager to libc's malloc, and use other debug tools.

If other options are exhausted, it can be worthwhile doing this and using valgrind. Note that to use valgrind you need to turn on -gv.

Semester answered 14/10, 2012 at 12:36 Comment(2)
can one just install FastMM4 into FPC and use those calls/vars he got used to in Delphi ?Infante
Never tried. I assume not without heavy modification (parsing debug info for backtraces symbolic info is probably totally different), and certainly it is not architecture dependent. I'm not aware of people that have even tried.Semester

© 2022 - 2024 — McMap. All rights reserved.