[fpc-pascal] memory leak detection using FPC

Graeme Geldenhuys graemeg.lists at gmail.com
Tue Sep 19 09:33:26 CEST 2006


So what is wrong with this code then...  When I quit the app, it says
I have one memory leak. What am I not freeing?

---------------------------------------------
program memleak;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils;

type
  TMyObject = class(TObject)
  private
    FAge: integer;
    FName: string;
  public
    property Name: string read FName write FName;
    property Age: integer read FAge write FAge;
  end;

var
  lObj: TMyObject;

begin
  lObj := TMyObject.Create;
  try
    lObj.Name := 'Graeme';
    lObj.Age := 123;
    Writeln('Hello ' + lObj.Name + ', you are ' + IntToStr(lObj.Age) +
' years old.');
  finally
    lObj.Free;
  end;
end.
---------------------------------------------

The output:

---------------------------------------------
[graemeg-linux] memoryleakdetection > ./memleak
Hello Graeme, you are 123 years old.
Heap dump by heaptrc unit
24 memory blocks allocated : 471/520
23 memory blocks freed     : 431/480
1 unfreed memory blocks : 40
True heap size : 393216 (32 used in System startup)
True free heap : 393072
Should be : 393088
Call trace for block $B7EB61B0 size 40
  $08070E6D
  $080480AB
---------------------------------------------

Compiler options:    -S2cgi -OG1 -gl -gh -vewnhi -l -Fu. -omemleak

Regards,
  - Graeme -



On 19/09/06, Michael Van Canneyt <michael at freepascal.org> wrote:
> Just add -gh to the compiler command-line.
> Run your project from a console, so you can see standard output.
> When the program is finished, it will give you a detailed list of memory leaks.
>
> Michael.

-- 
There's no place like 127.0.0.1



More information about the fpc-pascal mailing list