多谢各位!最后解决如下:
type
TInterfaceState = (isDestroying, isDestroyed);
TInterfaceStates = set of TInterfaceState;
INoneRefInterface = interface(IInterface)
['{3C606524-716A-4198-B5B6-B2637AB16216}']
procedure FreeNotifation; stdcall;
end;
TWindows = class(TInterfacedObject, IInterface, INoneRefInterface)
private
FInterfaceStates: TInterfaceStates;
protected
// Interface IInterface implement
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
// Interface INoneRefInterface implement
procedure FreeNotifation; stdcall;
public
constructor Create(...);
destructor Destroy; override;
end;
implementation
constructor TWindows.Create(...);
begin
inherited ;
FInterfaceStates := [];
// ...
end;
destructor TWindows.Destroy;
begin
Exclude(FInterfaceStates, isDestroying);
Include(FInterfaceStates, isDestroyed);
// ...
inherited ;
end;
function TWindows._AddRef: Integer;
begin
Result := 1;
end;
function TWindows._Release: Integer;
begin
if isDestroying in FInterfaceStates then
begin
Result := 0;
Free;
end
else if isDestroyed in FInterfaceStates then
Result := 0
else
Result := 1;
end;
procedure TWindows.FreeNotifation;
begin
Include(FInterfaceStates, isDestroying);
end;
// 使用时
var Windows: IWindows;
begin
Windows := CoWindows.Create(...);
Windows.Append(...);
Windows.Append(...);
(Windows as INoneRefInterface).FreeNotifation; // 真正需要释放对象时
// 否则只需将其赋为nil,_Release一下就可以了
Windows := nil;
end;