不是说接口是自动引用计数的吗, 为什么窗口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.
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.
解决方案 »
- 将可视控件COPY到别一个窗体
- 关于按多个仁值查询
- vfw视频解压缩问题!!对我来说太难了
- 老婆总咬我,不知道女人是什么心理,兄弟们帮忙分析一下。
- 程序的空该怎么填? 为什么?
- 我想画屏幕,怎么能得到屏幕的Canvas?
- 多层分布式,多表更新的代码??????????
- 菜鸟的问题?TChart 和TDBChart为什么这样不能工作。
- 如何使调用的外部应用程序满屏显示?
- 请问如何安装个组件?为什么我安装个组件后,每次运行后都说找不到该组件的.dcu文件,而我必须是重新Install Component后才可使用,还有我下了个XPMenu,怎么用?
- 求SQL语句一个
- 如何从下向上平滑滚动一个文本文件,AAfont自动换行有bug.
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.
其实在源码里面也可以很明显看出来,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方法吧.