delphi - Local variable broken by closure capture when accessed in nested method -


i've managed reduce problem :

program project1; {$apptype console}  uses   sysutils, threading;  procedure foo(astring: string); var   ltask : itask;   capturedstring : string;   procedure nested;   begin     try       writeln('nested : ' + capturedstring); { ! eintoverflow (win32) here }     except on e : exception       writeln(e.message);       end;    end; begin   capturedstring := astring;   writeln('local : ' + capturedstring);   nested;   ltask := ttask.create(     procedure       procedure anonnested;       begin         writeln(capturedstring); { removing eliminates problem }       end;     begin     end); end;  begin   foo('foo');   readln; end. 

here capturedstring variable gets corrupted when accessed within nested method. win32 compile raises eintoverflow, win64 compile writes out (corrupt) empty string - either build can provoked av or other exceptions manipulation in cases reference local variable corrupted when entering nested procedure.

this seems happen if capturedstring captured in closure.

what's going wrong?

this seems compiler bug:

#rsp-18833: capture closure corrupts local variable used in nested method

a workaround use second variable capture in anonymous method:

procedure foo(astring: string); var   ltask : itask;   capturedstring, s2 : string;   procedure nested;   begin     try       writeln('nested : ' + capturedstring);     except on e : exception       writeln(e.message);  { !!! }     end;    end; begin   capturedstring := astring;   s2 := capturedstring;   writeln('local : ' + capturedstring);   nested;   ltask := ttask.create(     procedure       procedure anonnested;       begin         writeln(s2); { capture variable }       end;     begin     end); end; 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -