6

在阅读了 StackOverflow 上关于对接口使用自动引用计数的缺点的许多帖子后,我开始尝试手动引用计数每个接口实例。

在尝试了一个完整的下午后,我放弃了!

为什么我在调用 FreeAndNil(p) 时会出现访问冲突?

下面是我的简单单元的完整列表。

unit fMainForm;

interface

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

type
  TForm4 = class(TForm)
    btn1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  end;

type
  IPersona = interface(IInterface)
  ['{44483AA7-2A22-41E6-BA98-F3380184ACD7}']
    function GetNome: string;
    procedure SetNome(const Value: string);
    property Nome: string read GetNome write SetNome;
  end;

type
  TPersona = class(TObject, IPersona)
  strict private
    FNome: string;
    function GetNome: string;
    procedure SetNome(const Value: string);
  protected
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  public
    constructor Create(const ANome: string);
    destructor Destroy; override;
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
end;

procedure TForm4.btn1Click(Sender: TObject);
var
  p: IPersona;
begin
  p := TPersona.Create('Fabio');
  try
    ShowMessage(p.Nome);
  finally
    FreeAndNil(p);
  end;
end;

constructor TPersona.Create(const ANome: string);
begin
  inherited Create;
  FNome := ANome;
end;

destructor TPersona.Destroy;
begin
  inherited Destroy;
end;

function TPersona._AddRef: Integer;
begin
  Result := -1
end;

function TPersona._Release: Integer;
begin
  Result := -1
end;

function TPersona.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then
    Result := S_OK
  else
    Result := E_NOINTERFACE;
end;

function TPersona.GetNome: string;
begin
  Result := FNome;
end;

procedure TPersona.SetNome(const Value: string);
begin
  FNome := Value;
end;

end.
4

2 回答 2

11

发生访问冲突是因为FreeAndNil接收到预期为对象引用的无类型 var 参数。您正在传递一个不符合要求的接口引用。不幸的是,您只能在运行时发现。在我看来,这是反对使用FreeAndNil.

您的引用计数通过接口引用计数机制禁用生命周期管理。为了销毁一个对象,你需要调用它的析构函数。为了做到这一点,您必须有权访问析构函数。您的界面不公开析构函数(也不应该公开)。因此,我们可以推断,为了销毁对象,您需要有一个对象引用。

以下是一些选项:

var
  obj: TPersona;
  intf: IPersona;
....
obj := TPersona.Create('Fabio');
try
  intf := obj;
  //do stuff with intf
finally
  obj.Free;
  // or FreeAndNil(obj) if you prefer
end;

或者你可以这样做

var
  intf: IPersona;
....
intf := TPersona.Create('Fabio');
try
  //do stuff with intf
finally
  (intf as TObject).Free;
end;
于 2012-10-30T08:59:19.773 回答
0

您不能FreeAndNil()与接口引用一起使用,只能与对象引用一起使用。如果您启用了接口的引用计数,您只需分配nil给接口引用(或让它超出范围)以正确释放对象,例如:

type
  TPersona = class(TInterfacedObject, IPersona)
  strict private
    FNome: string;
    function GetNome: string;
    procedure SetNome(const Value: string);
  public
    constructor Create(const ANome: string);
    destructor Destroy; override;
  end;

procedure TForm4.btn1Click(Sender: TObject);
var
  p: IPersona;
begin
  p := TPersona.Create('Fabio');
  try
    ShowMessage(p.Nome);
  finally
    p := nil;
  end;
end;

但是由于您已禁用接口上的引用计数,因此您需要在代码中重新使用普通对象引用变量,例如:

procedure TForm4.btn1Click(Sender: TObject);
var
  p: TPersona;
  intf: IPersona;
begin
  p := TPersona.Create('Fabio');
  try
    if Supports(p, IPersona, intf) then
      ShowMessage(intf.Nome);
  finally
    FreeAndNil(p);
  end;
end;
于 2012-10-30T17:48:42.940 回答