delphi – 在嵌套方法中访问时由闭包捕获破坏的局部变量
发布时间:2020-12-15 04:09:06 所属栏目:大数据 来源:网络整理
导读:我设法将此问题减少到: program Project1;{$APPTYPE CONSOLE}uses SysUtils,Threading;procedure Foo(AString: string);var LTask : ITask; capturedString : string; procedure Nested; begin try WriteLn('Nested : ' + capturedString); { ! EIntOverflo
我设法将此问题减少到:
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 do WriteLn(E.Message); end; end; begin capturedString := AString; WriteLn('Local : ' + capturedString); Nested; LTask := TTask.Create( procedure procedure AnonNested; begin WriteLn(capturedString); { Removing this eliminates the problem } end; begin end); end; begin Foo('foo'); ReadLn; end. 从嵌套方法中访问时,captureString变量在这里被破坏. Win32编译引发EIntOverflow,Win64编译写出一个(损坏的)空字符串 – 可以通过某种操作将构建引入AV或其他异常,但在所有情况下,在进入嵌套过程时对本地变量的引用都会被破坏. 这似乎只有在闭包中捕获captureString时才会发生. 出了什么问题? 解决方法
这似乎是一个编译器错误:
#RSP-18833: Capture by closure corrupts local variable used in nested method 解决方法是在匿名方法中使用第二个变量进行捕获: procedure Foo(AString: string); var LTask : ITask; capturedString,s2 : string; procedure Nested; begin try WriteLn('Nested : ' + capturedString); except on E : Exception do WriteLn(E.Message); { !!! } end; end; begin capturedString := AString; s2 := capturedString; WriteLn('Local : ' + capturedString); Nested; LTask := TTask.Create( procedure procedure AnonNested; begin WriteLn(s2); { Capture another variable } end; begin end); end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |