#1 2021-01-04 09:01:31

MikaK
Member
Registered: 2016-11-09
Posts: 14

Delphi 10.4 and TAutoFree

Hi,
We are considering to upgrade from 10.3.1 to 10.4.x
Is TAutoFree working in 10.4 as expected?

Offline

#2 2021-01-04 09:05:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,534
Website

Re: Delphi 10.4 and TAutoFree

I don't see why it shouldn't.

Offline

#3 2021-01-04 13:26:09

MikaK
Member
Registered: 2016-11-09
Posts: 14

Re: Delphi 10.4 and TAutoFree

unassigned interface returned as function parameter is freeed much earlier not in end of method
For example
https://quality.embarcadero.com/browse/RSP-30050

Offline

#4 2021-01-04 13:29:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,534
Website

Re: Delphi 10.4 and TAutoFree

I didn't know that.

What you could is use a "with ... do" as with FPC.
Could you try to use "with"?

If calling an interface method is enough, we may add a do-nothing method call for Delphi 10.4...

Offline

#5 2021-01-04 13:42:53

MikaK
Member
Registered: 2016-11-09
Posts: 14

Re: Delphi 10.4 and TAutoFree

Okay - haven't yet installed 10.4... big_smile Probably haveto big_smile and test

Offline

#6 2021-01-04 14:30:35

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,534
Website

Re: Delphi 10.4 and TAutoFree

This early release "new feature" breaks the TAutoFree and TSQLRecord.AutoFree expectations.
I have documented it properly on mORMot 2 to state that it should perhaps not be used any more for new projects, since it is also not supported by Delphi.
Also TSynLog.Enter() returns a ISynLog which should be assigned to a local variable...

I have added an interface method call for Delphi 10.4+ on mORMot2, trying to circumvent the TAutoFree.One() problem...
Check https://github.com/synopse/mORMot2/comm … 183d77202b

Any additional feedback is welcome!

Offline

#7 2021-01-04 17:57:36

tbo
Member
Registered: 2015-04-20
Posts: 88

Re: Delphi 10.4 and TAutoFree

I found the following change in the source code.

procedure TAutoFree.Another(var localVariable; obj: TObject);
...
-  for i := high(fObjectList) downto 0 do // release FILO
+  for i := length(fObjectList) - 1 downto 0 do // release FILO

Arnaud, would it be possible to explain me the reason for this? Is there anything to keep in mind when using High()?

With best regards
Thomas

Offline

#8 2021-01-04 18:30:53

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,534
Website

Re: Delphi 10.4 and TAutoFree

On some compilers - e.g. FPC - length() is slightly faster than high().

Offline

#9 2021-01-12 11:14:27

Eugene Ilyin
Member
From: milky_way/orion_arm/sun/earth
Registered: 2016-03-27
Posts: 128
Website

Re: Delphi 10.4 and TAutoFree

@ab,
Please consider the another alternative to fix it in the recent Delphi compilers with Managed Record inside TAutoFree.
One of the benefit from-the-box is reverse auto-invocation order of Finalize() in routines epilogue.

Offline

#10 2021-01-12 17:37:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,534
Website

Re: Delphi 10.4 and TAutoFree

The next logical step from Delphi may be to "optimize" the managed record finalization, just as they did for interface finalization...
So there is no guarantee they would be a safer alternative.

Offline

#11 2021-09-21 09:49:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,534
Website

Re: Delphi 10.4 and TAutoFree

Offline

#12 2021-09-22 13:03:50

Stefan
Member
Registered: 2012-11-23
Posts: 26

Re: Delphi 10.4 and TAutoFree

Eugene Ilyin wrote:

Please consider the another alternative to fix it in the recent Delphi compilers with Managed Record inside TAutoFree.

Will not work - see this code:

type
  autofree = record
    class operator finalize(var value: autofree);
  end;

class operator autofree.finalize(var value: autofree);
begin
  Writeln('finalize');
end;

function get: autofree;
begin
end;

procedure Main(const i: Integer);
begin
  if i = 42 then
    get;
  Writeln('end');
end;

begin
  Main(42);
  Readln;
end.

Will write 'finalize' before 'end'

Offline

#13 2021-09-22 16:13:24

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 12,534
Website

Re: Delphi 10.4 and TAutoFree

Thanks for the input.

I was indeed guessing records would have the same lifetime than other managed types.

Offline

#14 2021-09-25 18:02:59

Eugene Ilyin
Member
From: milky_way/orion_arm/sun/earth
Registered: 2016-03-27
Posts: 128
Website

Re: Delphi 10.4 and TAutoFree

Yeah, but it was so usefull not to generate all this try ... finally nesting, all libraries and all sources with IAutoFree now must be rewritten.
Especially for TSQLRecord and it's ancesstors sad

Offline

Board footer

Powered by FluxBB