【发布时间】:2017-01-01 15:46:29
【问题描述】:
我有一个以参数为对象的方法(下面的代码片段):
TMyObject=class(TObject)
constructor Create();
destructor Destroy();override;
end;
implementation
function doSomething(x:TMyObject):integer;
begin
//code
end;
procedure test();
var
w:integer;
begin
w:=doSomething(TMyObject.Create);
//here: how to free the created object in line above?
end;
如何销毁在被调用方法doSomething内创建的对象?
【问题讨论】:
-
别偷懒。创建一个变量!
-
@DalijaPrasnikar stackoverflow.com/a/7640979/505088
-
@DalijaPrasnikar stackoverflow.com/a/4510268今天的力量与我同在!
-
@DalijaPrasnikar,仅当接口参数为
const时才会出现泄漏。我说的对吗? -
@kobik:你也可以使用
const,但是如果你将它传递为doSomething(TMyObject.Create);,它会泄漏。如果您将其传递为doSomething(TMyObject.Create as IMyInterface);,它将不会泄漏。我总是使用后一种形式。