接口基类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 若犯了啥低级错误欢迎大虾们批评指出 嘿嘿
...全文
188 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代码,或许会有不少帮助

源码下载地址: https://pan.quark.cn/s/0d6e3002ba51 ### IR2110中文资料知识点详述 #### 一、概述 IR2110是一种专为脉冲宽度调制应用而设计的高性能驱动集成电路,在各类功率转换设备中具有广泛的应用,特别是在需要实现隔离驱动的场景下表现出色。该芯片融合了光电隔离与电磁隔离的优势,能够达成快速响应并保持较小的物理尺寸,尤其适合用于中小功率的变换装置。 #### 二、IR2110内部结构与特点 **1. 内部结构** - **封装形式**:采用DIP14引脚封装。 - **内部组成**:由逻辑输入单元、电平平移单元以及输出保护单元这三个主要部分构成。 - **悬浮电源**:借助自举电路实现,使得高端工作电压最高能够达到500V,支持±50V/ns的电压变化率。 - **工作电压范围**:逻辑电源电压范围为5~15V,输出电源端电压范围为10~20V,易于与其他逻辑电平进行匹配。 - **工作频率**:最高可达到500kHz。 - **功耗**:在15V条件下静态功耗仅为116mW。 - **延迟时间**:导通延迟120ns,关断延迟94ns。 - **峰值输出电流**:图腾柱输出峰值电流为2A。 **2. 特点** - **独立输入通道**:具备独立的低端和高端输入通道配置。 - **兼容性**:逻辑电源地与功率地之间允许±5V的偏移量,便于与TTL或CMOS电平进行匹配。 - **自举电源**:内置自举电源电路,有效减少驱动电源的数量,从而简化电路设计。 - **高dv/dt能力**:具有较强的dv/dt抑制能力,适合高频开关应用。 - **低功耗**:工作状态下功耗较低,有利于提升整体效率。 #### 三、高压侧...

16,741

社区成员

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

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