[fpc-pascal] methods of an object to create others objects

Graeme Geldenhuys graemeg.lists at gmail.com
Wed Jul 7 09:05:34 CEST 2010


Op 2010-07-07 00:14, Andrew Brunner het geskryf:
> 
> No, if the nesting is done properly you could guarantee that Obj2.Free
> would be called even if Obj1.Free failed with some exception.

No matter how many nested try..except or try..finally blocks you have, if
you are trying to free the instance and it causes an exception, you might
catch the exception, but you still can't free the object because it caused
an exception the first time, and will probably keep causing an exception
every time you try and free that instance. There is no way around that
memory leak because you simply can't free the instance.

Here is an example:
---------------[ memory_leak.pas ]------------------------
// compile with: fpc -gh -gl memory_leak.pas
program memory_leak;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils;

type
  TMyObject = class(TObject)
  private
    FList: TList;
    FMyName: string;
  public
    constructor Create;
    destructor Destroy; override;
    property MyName: string read FMyName write FMyName;
  end;

{ TMyObject }

constructor TMyObject.Create;
begin
  FList := TList.Create;
end;

destructor TMyObject.Destroy;
var
  i, j: integer;
begin
  i := 10;
  j := 0;
  j := i div j;
  FList.Free;
  inherited Destroy;
end;

var
  obj: TMyObject;

{$R *.res}

begin
  obj := TMyObject.Create;
  try
    try
      obj.MyName := 'Me';
      try
        writeln(obj.MyName);
      except
        on E: Exception do
          writeln('Oops'); // after this, the finally will still run
      end;
    finally
      obj.Free;
    end;
  except
    on E: Exception do
      obj.Free;   // this will just cause another exception
  end;
end.

------------------------------------------------



...and here is the output:

------------------------------------------------
$ ./memory_leak
Me
An unhandled exception occurred at $00000000004002C7 :
EDivByZero : Division by zero
  $00000000004002C7 line 32 of memory_leak.lpr
  $000000000040FB2A

Heap dump by heaptrc unit
29 memory blocks allocated : 1071/1104
23 memory blocks freed     : 819/848
6 unfreed memory blocks : 252
True heap size : 163840
True free heap : 162624
Should be : 162816
Call trace for block $00007F2DBBB6D1E0 size 128
  $0000000000415CAA
  $0000000000411214
  $0000000000400CB0
  $000000000040FB2A
  $0000000000400178
Call trace for block $00007F2DBBB8DB40 size 40
  $0000000000411214
  $0000000000400CB0
  $000000000040FB2A
  $0000000000400178
Call trace for block $00007F2DBBB8DA80 size 20
  $0000000000462F7E
  $0000000000400CB0
  $000000000040FB2A
  $0000000000400178
Call trace for block $00007F2DBBB8D840 size 24
  $000000000042DE9E
  $0000000000400216 line 23 of memory_leak.lpr
  $000000000040034F line 43 of memory_leak.lpr
  $0000000000400178
Call trace for block $00007F2DBBB853E0 size 16
  $0000000000400216 line 23 of memory_leak.lpr
  $000000000040034F line 43 of memory_leak.lpr
  $0000000000400178
Call trace for block $00007F2DBBB8D780 size 24
  $000000000040034F line
------------------------------------------------

Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/




More information about the fpc-pascal mailing list