Delphi 接口中的引用计数
Reference counting in Delphi Interfaces
基本问题。
在 Button1Click 中,我创建了一个接口对象。创建后引用计数为0。
我将对象作为参数传递。引用计数在函数结束时增加、减少,当它为 0 时,它被销毁。我想念什么吗?当我首先创建对象时,我在想引用计数应该是 1? lListFilter 没有持有对象的引用?
type
IPersistentListFilter = Interface(IInterface)
['{57cdcf89-60ee-4b3c-99fd-177b4b98d7e5}']
procedure IncludeObject;
end;
procedure FillList(AFilter : IPersistentListFilter);
type
TPersistentListFilter = class(TInterfacedObject, IPersistentListFilter)
procedure IncludeObject;
constructor Create;
destructor Destroy; override;
end;
implementation
procedure FillList(AFilter: IPersistentListFilter);
begin
AFilter.IncludeObject;
end;
constructor TPersistentListFilter.Create;
begin
inherited;
end;
destructor TPersistentListFilter.Destroy;
begin
inherited;
end;
procedure TPersistentListFilter.IncludeObject;
begin
// do nothing
end;
procedure TForm8.Button1Click(Sender: TObject);
var
lListFilter: TPersistentListFilter;
begin
lListFilter := TPersistentListFilter.Create;
// ref count is 0
FillList(lListFilter);
// lListFilter has been destroyed
FillList(lListFilter); // --> error
end;
在 Button1Click
中,lListFilter
被声明为 TPersistentListFilter
的实例,而不是 IPersistentListFilter
。因此,在创建 lListFilter
时不会发生引用计数。
lListFilter
需要声明为 IPersistentListFilter
:
procedure TForm8.Button1Click(Sender: TObject);
var
lListFilter: IPersistentListFilter;
begin
lListFilter := TPersistentListFilter.Create;
// ref count will be 1
// ref count will go to 2 during call to FillList
FillList(lListFilter);
// ref count will be back to 1
// ref count will go to 2 during call to FillList
FillList(lListFilter);
// ref count will be back to 1
end; // ref count will go to 0 as lListFilter goes out of scope
// and is destroyed.
基本问题。 在 Button1Click 中,我创建了一个接口对象。创建后引用计数为0。 我将对象作为参数传递。引用计数在函数结束时增加、减少,当它为 0 时,它被销毁。我想念什么吗?当我首先创建对象时,我在想引用计数应该是 1? lListFilter 没有持有对象的引用?
type
IPersistentListFilter = Interface(IInterface)
['{57cdcf89-60ee-4b3c-99fd-177b4b98d7e5}']
procedure IncludeObject;
end;
procedure FillList(AFilter : IPersistentListFilter);
type
TPersistentListFilter = class(TInterfacedObject, IPersistentListFilter)
procedure IncludeObject;
constructor Create;
destructor Destroy; override;
end;
implementation
procedure FillList(AFilter: IPersistentListFilter);
begin
AFilter.IncludeObject;
end;
constructor TPersistentListFilter.Create;
begin
inherited;
end;
destructor TPersistentListFilter.Destroy;
begin
inherited;
end;
procedure TPersistentListFilter.IncludeObject;
begin
// do nothing
end;
procedure TForm8.Button1Click(Sender: TObject);
var
lListFilter: TPersistentListFilter;
begin
lListFilter := TPersistentListFilter.Create;
// ref count is 0
FillList(lListFilter);
// lListFilter has been destroyed
FillList(lListFilter); // --> error
end;
在 Button1Click
中,lListFilter
被声明为 TPersistentListFilter
的实例,而不是 IPersistentListFilter
。因此,在创建 lListFilter
时不会发生引用计数。
lListFilter
需要声明为 IPersistentListFilter
:
procedure TForm8.Button1Click(Sender: TObject);
var
lListFilter: IPersistentListFilter;
begin
lListFilter := TPersistentListFilter.Create;
// ref count will be 1
// ref count will go to 2 during call to FillList
FillList(lListFilter);
// ref count will be back to 1
// ref count will go to 2 during call to FillList
FillList(lListFilter);
// ref count will be back to 1
end; // ref count will go to 0 as lListFilter goes out of scope
// and is destroyed.