不是说接口是自动引用计数的吗, 为什么窗口2不释放//Unit1
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
  private
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
var
  iTestIntf: ITest;
begin
  Form2 := TForm2.Create(nil);
  Form2.Show;
  iTestIntf := Form2 as ITest;
  iTestIntf.DoTest;//iTestIntf最后一次使用, 为什么窗口2还显示在那不释放呢
end;end.
//Unit2
unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;type
  ITest = interface
    ['{5E71E68A-167A-4051-B294-19270A6B63FE}']
    procedure DoTest;
  end;  TForm2 = class(TForm, ITest)
  private
    { Private declarations }
  public
    procedure DoTest;
  end;var
  Form2: TForm2;implementation{$R *.dfm}{ TForm2 }procedure TForm2.DoTest;
begin
  ShowMessage('Test');
end;end.

解决方案 »

  1.   

    你没有实现_AddRef与_Release接口,所以你所有创建的资源仍然需要自己去释放
      

  2.   

    TComponent已经实现这两个方法啊
      

  3.   

    TComponent实现的_AddRef与_Release只对com象有用
    TForm并不适用
    Called when an application uses a component interface.Delphi syntax:function _AddRef: Integer; stdcall;C++ syntax:int __stdcall _AddRef(void);Description_AddRef is a basic implementation of the IInterface method, AddRef.If the component is a wrapper for a COM object, _AddRef calls the AddRef method of that COM object, and returns the resulting reference count.In all other cases, _AddRef simply returns –1 and takes no action. This allows the component to implement interfaces where reference counting is not required. More sophisticated components should override _AddRef to implement reference counting.
      

  4.   

    楼上的已经说了..
    其实在源码里面也可以很明显看出来,TComponent加入引用计数方法目的是封装COM,
    而不是用于自己维持引用计数.它自己不包含计数的值.
    function TComponent._AddRef: Integer;
    begin
      if FVCLComObject = nil then
        Result := -1   // -1 indicates no reference counting is taking place
      else
        Result := IVCLComObject(FVCLComObject)._AddRef;
    end;function TComponent._Release: Integer;
    begin
      if FVCLComObject = nil then
        Result := -1   // -1 indicates no reference counting is taking place
      else
        Result := IVCLComObject(FVCLComObject)._Release;
    end;
    楼主自己定义_AddRef与_Release方法吧.