我對釋放接口對象有疑問。釋放接口對象
我的界面看起來是這樣的:
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;
我得到了一些clases從以前的類繼承和也實現了界面。
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;
我的問題是,我如何摧毀這個輔助對象,它是一個廣積interfacedObject? 我想這件事,首先檢查這不是一個零對象:
(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類繼承的析構函數。
您需要爲'destructor'聲明添加'override;'。除此之外,你不應該做任何明確的事情來釋放對象。 –
這就是......把它當成一個問題,我會給你最好的答案......謝謝! – Izuel
那還不夠。它仍然會失敗 –