[fpc-pascal] _Release call location for unused function results

Zoë Peterson zoe at scootersoftware.com
Mon Jan 29 23:32:37 CET 2018


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.




More information about the fpc-pascal mailing list