[fpc-pascal] memory leak detection using FPC
Michael Van Canneyt
michael at freepascal.org
Tue Sep 19 09:53:36 CEST 2006
On Tue, 19 Sep 2006, Graeme Geldenhuys wrote:
> 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?
Try putting everyting in a procedure 'Test' and run that.
Global variables (even temporary ones) may not yet have been deallocated
when the debugger output is written. By putting everything in a procedure,
you force deallocation of all used variables, implicit or not.
Michael.
>
> ---------------------------------------------
> 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
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list