我最近偶然发现了一个问题,这个问题是由我编写的一些非常旧的代码引起的,这些代码显然假设with语句中使用的接口引用将在with-block被保留后立即释放-有点像隐式的try-finally-block (如果我理解正确的话,类似于C#的using-statement )。
显然(在Delphi2009中)这不是(不再是?)这个案子。有人知道这是什么时候发生的吗?还是我的代码一开始就完全错了?
为了清楚起见,这里有一个简化的例子:
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每当有人开始陈旧的"with是邪恶的“论点时,这总是我脑海中的一个例子,它让我坚持说”是的,但是……“。似乎我错了..。有人能证实吗?
发布于 2009-08-05 15:06:39
Pascal/Delphi中的with保留字只用于方便地访问记录或对象/类的成员(即,为了不提及记录/对象/类的名称)。它与与垃圾收集相关的C# with非常不同。从records诞生之日起,它就存在于PASCAL语言中,以简化对许多数据成员(当时简称为“字段”)的代码调用。
总而言之,with与垃圾回收、内存释放或对象实例销毁无关。在with头构造的对象之前可能只是在单独的代码行中初始化,这是相同的。
发布于 2009-08-05 18:50:55
这种WITH-行为从未改变过。为了达到你的预期行为,你可以这样修改你的代码:
procedure TestIt;
var
myIntf: IMyIntf;
begin
DoSomething;
myIntf := TSomeObject.GetMyIntf
DoStuff;
DoMoreStuff;
myIntf := nil; // <- here is where TSomeObject.Destroy called
DoSomethingElse;
end; 或者,您可以在过程中执行此操作:
procedure TestIt;
procedure DoAllStuff;
var
myIntf: IMyIntf;
begin
myIntf := TSomeObject.GetMyIntf
DoStuff;
DoMoreStuff;
end; // <- here is where TSomeObject.Destroy called
begin
DoSomething;
DoAllStuff;
DoSomethingElse;
end; https://stackoverflow.com/questions/1233773
复制相似问题