TInterfaceObject的引用计数的问题

babyboy 2005-08-05 06:32:44
ITest = Interface
['{4979F659-C4BA-4F01-B09F-A4E7AA5022F1}']
procedure Excute;
end;

TTest = (TInterfaceObject, ITest)
private
ID: integer;
public
Excute;
end;

var
tt:TTest;
procedure Create;
begin
if not assigned(tt) then tt := TTest.Create; //此时tt的引用计数怎么还是0?
Insert(tt); //tt的引用计数变为1
Remove(tt); //tt的引用计数变为0,被释放了
tt.Execute; //访问非法地址
end;



procedure Insert(aItest: ITest);
begin
FList.Insert(aItest); //FList是TInterfaceList;
end;

procedure Remove(aItest:ITest);
begin
FList.Remove(aItest);
end;

为什么tt被创建以后引用计数还是0?
...全文
104 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tobelost 2005-08-07
  • 打赏
  • 举报
回复
_AddRef
_Release

或者自己实现IUnknown接口的这两个函数,
直接 Result := -1;不实现计数也可以。
tobelost 2005-08-07
  • 打赏
  • 举报
回复
halfdream(哈欠) 正解

既然你的类实现了接口,就不要调用类来创建实例,
halfdream 2005-08-06
  • 打赏
  • 举报
回复
才创建的时候,没有INTERFACE变量引用它,这时候当然引用计数为0.
tt := TTest.Create;注意,这儿的tt类型是TTest而不是ITest!

当调用时候,
Insert(tt); //这时候才开始引用..
cdsgajxlp 2005-08-05
  • 打赏
  • 举报
回复
TTest = (TInterfaceObject, ITest)
private
ID: integer;
FRefCount: integer;
public
Function _AddRef : integer;Stdcall;
Function _Release : integer;Virtual;StdCall;
Excute;
end;

Function TTest._AddRef : Integer;
begin
Result:=InterLockedDerement(FRefCount)
end;

Function TTest._Release : integer;
begin
Result:=InterLockedDecrement(FRefCount);
end;


5,928

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧