Oli*_*sen 4 delphi interface reference-counting delphi-2009 with-statement
我最近偶然发现了一些由我编写的非常旧的代码引起的问题,这显然假设一个with
语句中使用的接口引用会在with
-block被保留后立即释放- 有点像一个隐式try-finally
块(类似于C#的using
-statement)如果我理解正确的话).
显然(在Delphi 2009中)这不是(不再是?)的情况.有谁知道这发生的时间?或者我的代码开始时是完全错误的?
为了澄清,这是一个简化的例子:
type
IMyIntf = interface;
TSomeObject = class(TInterfacedObject, IMyIntf)
protected
constructor Create; override; // creates some sort of context
destructor Destroy; override; // cleans up the context created in Create
public
class function GetMyIntf: IMyIntf; //a factory method, calling the constructor
end;
procedure TestIt;
begin
DoSomething;
with (TSomeObject.GetMyIntf) do
begin
DoStuff;
DoMoreStuff;
end; // <- expected: TSomeObject gets destroyed because its ref.count is decreased to 0
DoSomethingElse;
end; // <- this is where TSomeObject.Destroy actually gets called
Run Code Online (Sandbox Code Playgroud)
每当有人开始旧的" with
邪恶"论证时,这总是我心中的一个例子让我继续"是的,但......".好像我错了......有人能证实吗?
Roe*_*ler 17
该with
帕斯卡尔保留字/德尔福仅用于容易访问的记录或对象/类成员(即,为了更何况记录的/对象/类的名称).它与C#with
与垃圾收集有很大不同.它自records
诞生之日起就以Pascal语言存在,以简化代码调用许多数据成员(当时简称为"字段").
总而言之,with
与垃圾收集,内存释放或对象实例的破坏无关.在with
标题中构造的对象可能刚刚在单独的代码行中初始化,它们是相同的.
归档时间: |
|
查看次数: |
486 次 |
最近记录: |