Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline var bug with anonymous procedure?

Tags:

delphi

with the code below:

Procedure TMyForm.doAction;
begin

    generateToken;
    VAR LToken := generatedToken; // Ltoken is set correctly to xxxxx

    //-----
    TThread.CreateAnonymousThread(
      procedure
      begin

        GetData(LToken); // << here LToken is empty !!

      end).Start;

end;

As you can see, the value of LToken is empty inside the anonymous procedure (so the capture didn't work). if I don't use inline var for LToken then it's work

Procedure TMyForm.doAction;
Var LToken: ansiString;
begin
   ....
end;

Is this a bug or a know limitation ?

like image 758
zeus Avatar asked Jan 17 '26 06:01

zeus


1 Answers

There was a compiler bug in Delphi 10.3.x and inline variables are not properly captured by anonymous methods.

Inline variables don't work with anonymous methods https://quality.embarcadero.com/browse/RSP-26666

Solution for 10.3.x, as you already mentioned, is to define and use local variable instead of using inline variable.

Another solution would be upgrading to 10.4, where capturing inline variables with anonymous methods has been corrected and now it correctly captures inline variable, so you would be able to use your original code.

However, there is additional bug in compiler in 10.4 related to capturing inline variables. If the variable is managed type (interface reference, string...), it is never automatically released and it leaks memory.

Such variables must be explicitly released at the end of anonymous method that captured them. For instance, assigning nil to interface reference or assigning empty string to string variable...

Memory leak: inline Interfaces captured by anonymous methods are not released https://quality.embarcadero.com/browse/RSP-29564

like image 196
Dalija Prasnikar Avatar answered Jan 19 '26 20:01

Dalija Prasnikar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!