我有一个关于释放接口对象的疑问。
我的接口看起来像这样:
type IBase = interface(IInterface)
function test: Boolean;
end;
这个其他的类有一些属性,我希望在所有接口类中都可以使用:
type TBase = class(TInterfacedObject)
protected
FQuery: TADOQuery;
FADOConnection: TADOConnection;
public
constructor Create; virtual; abstract;
destructor Destroy;
end;
我有一些继承之前类并实现接口的类。
type TExample= class(TBase, IBase)
public
function test: Boolean;
destructor Destroy;
end;
所以,使用这种模式,我可以像下面这样使用类:
procedure someProcedure(aux : IBase);
begin
aux.test; //Aux is an instance of TExample and I'm using the interfaced method
end;
我的问题是,我如何销毁这个IBase接口的辅助对象?我已经尝试了一些方法,首先检查它不是一个空对象:
(aux as TObject).Destroy; //Invalid Pointer operation
(aux as TInterfacedObject).Destroy; //Invalid Pointer operation
(aux as TExample).Destroy; Also invalid Pointer operation!!??
我看到有人写道,因为该对象继承自TInterfacedObject并实现了接口,所以不能直接释放该对象,而应该使用以下方式:
aux := nil;
引用计数器会自动处理,但在我的项目中使用ReportMemoryLeaksOnShutdown := True;会导致一些泄漏并且代码永远无法到达析构函数的断点。
我有什么遗漏吗?
编辑:
我修改了我的构造函数,现在像这样:
type TBase = class(TInterfacedObject)
protected
FQuery: TADOQuery;
FADOConnection: TADOConnection;
public
constructor Create;
destructor Destroy; override;
end;
type TExample= class(TBase, IBase)
public
function test: Boolean;
end;
现在我猜这有更多意义,因为TBase正在分配和释放对象,而TExample类继承了析构函数。
override;
。除此之外,你不需要进行任何显式的操作来释放对象。 - 500 - Internal Server Error