In the code below Bar creates a reference counted object and returns an interface to it. Foo calls Bar but doesn't assign the result anywhere.

In Delphi, this produces the result:
  Foo >
  TAutoRelease.Create
  Foo <
  TAutoRelease.Destroy

But in Free Pascal it produces this:
  Foo >
  TAutoRelease.Create
  TAutoRelease.Destroy
  Foo <

If I change Foo to assign the interface to a local variable but don't do anything with that variable, Free Pascal produces the same results as Delphi.

Delphi's behavior is what I expected, and allows tricks with triggering behavior when leaving the current scope. Barry Kelly has one example on his blog where he talks about implementing C++'s RAII using it: http://blog.barrkel.com/2010/01/one-liner-raii-in-delphi.html

Is Free Pascal's behavior intentional?  Can it be changed to match Delphi?

Thanks,
Zoë Peterson
Scooter Software


============================================

program AutoRelease;

{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
{$APPTYPE CONSOLE}

uses
  Classes,
  SysUtils;

type
  TAutoRelease = class(TInterfacedObject)
    constructor Create;
    destructor Destroy; override;
  end;

constructor TAutoRelease.Create;
begin
  WriteLn('TAutoRelease.Create');
end;

destructor TAutoRelease.Destroy;
begin
  WriteLn('TAutoRelease.Destroy');
  inherited;
end;

function Bar: IInterface;
begin
  Result := TAutoRelease.Create;
end;

procedure Foo;
begin
  WriteLn('Foo >');
  Bar;
  WriteLn('Foo <');
end;

begin
  Foo;
end.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to