[fpc-pascal] _Release call location for unused function results
Michael Van Canneyt
michael at freepascal.org
Tue Jan 30 08:34:44 CET 2018
On Mon, 29 Jan 2018, Zoƫ Peterson wrote:
> 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?
The question is whether Delphi's behaviour is intentional. You are relying
on undocmented behaviour: when does an intermediate temp variable go out of scope?
This is not defined, and the behaviours of Delphi and FPC regarding temp
variables do not match. This is well known since many many years.
see e.g.
https://bugs.freepascal.org/view.php?id=30409
As Maciej wrote, it can be changed.
But relying on what is an implementation detail is ill-advised, and
certainly not robust programming.
Michael.
More information about the fpc-pascal
mailing list