接口基类IUnknown(Interface)整型变量引用计数FRefCount(整型变量)初始化的疑惑

caixiaobai08 2009-07-17 04:18:14
《delphi com深入编程》一书第一章第9页中 人工实现接口基类 代码如下

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;

IFormattedNumber = interface
function FormattedString : String;
end;

TFormattedInteger = class(TObject,IFormattedNumber)
private
FValue : Integer;
FRefCount : Integer;//引用计数
public
constructor Create(AValue : Integer);
//IUnknown 接口
function QueryInterface(const IID : TGUID;out Obj): HResult; stdcall;
function _AddRef : Integer; Stdcall;
function _Release : Integer; stdcall;
//IFormattedNumber 接口
function FormattedString : String;
procedure SetValue(AValue : Integer);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
constructor TFormattedInteger.Create(AValue : Integer);
begin
inherited Create;
FValue := AValue;
end;

function TFormattedInteger.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
const
E_NOINTERFACE = $80004002;
begin
if GetInterface(IID,Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;

function TFormattedInteger._AddRef: Integer;stdcall;
begin
Inc(FRefCount);
Result := FRefCount;
end;

function TFormattedInteger._Release: Integer; stdcall;
begin
Dec(FRefCount);
Result := FRefCount;
If FRefCount = 0 then
Destroy;
end;

//IFormattedNumber接口
function TFormattedInteger.FormattedString : String;
begin
Result := 'The value is' + IntToStr(FValue);
end;

procedure TFormattedInteger.SetValue(AValue : Integer);
begin
FValue := AValue;
end;

end.

使用接口时貌似FRefCount初始值为0,这样Inc(FRefCount)=1,Dec(FRefCount)=0 接口使用完了后才能释放,但小弟在bottonclick事件中显示一个未初始化整型变量发现其值貌似不定,望大虾不吝赐教,帮小弟解惑,3Q
ps:小弟初学delphi 若犯了啥低级错误欢迎大虾们批评指出 嘿嘿
...全文
147 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
caixiaobai08 2009-07-18
  • 打赏
  • 举报
回复
轻轻顶下
caixiaobai08 2009-07-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sonicer 的回复:]
class function TInterfacedObject.NewInstance: TObject;
begin
  Result := inherited NewInstance;
  TInterfacedObject(Result).FRefCount := 1;
end;
是不是这个起的作用?
[/Quote]
我觉得是 难怪说要从TInterfacedObject继承 哪位大虾来指点下 对不对
caixiaobai08 2009-07-17
  • 打赏
  • 举报
回复
还有TInterfacedObject._AddRef的源码

function TInterfacedObject._AddRef: Integer;
begin
Result := InterlockedIncrement(FRefCount);
end;
林石公 2009-07-17
  • 打赏
  • 举报
回复
class function TInterfacedObject.NewInstance: TObject;
begin
Result := inherited NewInstance;
TInterfacedObject(Result).FRefCount := 1;
end;
是不是这个起的作用?
caixiaobai08 2009-07-17
  • 打赏
  • 举报
回复
还有TInterfacedObject._AddRef的源码

function TInterfacedObject._AddRef: Integer;
begin
Result := InterlockedIncrement(FRefCount);
end;
caixiaobai08 2009-07-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sonicer 的回复:]
一般在Delphi中,需要使用接口时,是从TInterfacedObject来生成类的,这个类中实现了引用计数的功能,
TFormattedInteger = class(TInterfacedObject,IFormattedNumber);
这样在这个类中,你就不需要考虑和实现下边三个函数了
function QueryInterface(const IID : TGUID;out Obj): HResult; stdcall;
function _AddRef : Integer; Stdcall;
function _Release : Integer; stdcall;

如下,
TFormattedInteger = class(TInterfacedObject,IFormattedNumber)
  private
    FValue : Integer;
    FRefCount : Integer;//引用计数
  public
    constructor Create(AValue : Integer);
    //IFormattedNumber 接口
    function FormattedString : String;
    procedure SetValue(AValue : Integer);
  end;
这样你只要关心自己的业务实现就可以了,至于引用计数,由TInterfaceObject来替你完成好了

不过你要是真的关心引用计数的实现,你也可以看TInterfacedObject的vcl代码,或许会有不少帮助


[/Quote]
我找到了System.pas中_AddRef函数的源码
procedure _AddRef{ p: Pointer; typeInfo: Pointer};
asm
MOV ECX,1
JMP _AddRefArray
end;
或许是不是ECX寄存器的初值是0?
caixiaobai08 2009-07-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 smallhand 的回复:]
bottonclick写错了ButtonClick.你的ButtonClick怎么写的?

[/Quote]不好意思 是我打错了
林石公 2009-07-17
  • 打赏
  • 举报
回复
在TInterfacedObject中有以下几个函数/过程,或许可以解释关于FRefCount的问题
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
class function NewInstance: TObject; override;
火龙岛主 2009-07-17
  • 打赏
  • 举报
回复
bottonclick写错了ButtonClick.你的ButtonClick怎么写的?
林石公 2009-07-17
  • 打赏
  • 举报
回复
一般在Delphi中,需要使用接口时,是从TInterfacedObject来生成类的,这个类中实现了引用计数的功能,
TFormattedInteger = class(TInterfacedObject,IFormattedNumber);
这样在这个类中,你就不需要考虑和实现下边三个函数了
function QueryInterface(const IID : TGUID;out Obj): HResult; stdcall;
function _AddRef : Integer; Stdcall;
function _Release : Integer; stdcall;

如下,
TFormattedInteger = class(TInterfacedObject,IFormattedNumber)
private
FValue : Integer;
FRefCount : Integer;//引用计数
public
constructor Create(AValue : Integer);
//IFormattedNumber 接口
function FormattedString : String;
procedure SetValue(AValue : Integer);
end;
这样你只要关心自己的业务实现就可以了,至于引用计数,由TInterfaceObject来替你完成好了

不过你要是真的关心引用计数的实现,你也可以看TInterfacedObject的vcl代码,或许会有不少帮助

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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