我有一个带有对象参数的方法(以下是示例代码):
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 之外销毁在该方法内创建的对象?
const
时才会发生泄漏。我说得对吗? - kobikconst
,但如果你将其作为doSomething(TMyObject.Create);
传递,它会泄漏。 如果你将其作为doSomething(TMyObject.Create as IMyInterface);
传递,它不会泄漏。 我总是使用后者的形式。 - Rudy Velthuis