Questions tagged «delphi-2010»

2
是否记录了编译器对隐式接口变量的处理?
我不久前问了一个关于隐式接口变量的类似问题。 这个问题的源头是我的代码中的一个错误,因为我没有意识到编译器创建的隐式接口变量的存在。拥有它的过程完成时,此变量已完成。反过来,由于变量的生存期比我预期的长,导致了一个错误。 现在,我有一个简单的项目来说明编译器的一些有趣行为: program ImplicitInterfaceLocals; {$APPTYPE CONSOLE} uses Classes; function Create: IInterface; begin Result := TInterfacedObject.Create; end; procedure StoreToLocal; var I: IInterface; begin I := Create; end; procedure StoreViaPointerToLocal; var I: IInterface; P: ^IInterface; begin P := @I; P^ := Create; end; begin StoreToLocal; StoreViaPointerToLocal; end. StoreToLocal就像您想象的那样被编译。I函数的结果局部变量作为隐式var参数传递给Create。整理StoreToLocal一次即可调用IntfClear。没有惊喜。 但是,StoreViaPointerToLocal区别对待。编译器会创建一个隐式局部变量,并将其传递给Create。当Create返回时,对分配P^进行。这使例程具有两个局部变量,这些局部变量保存对接口的引用。整理StoreViaPointerToLocal导致两次致电IntfClear。 的编译代码StoreViaPointerToLocal如下: ImplicitInterfaceLocals.dpr.24: …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.