为什么方法退出时这个接口没有正确释放?
Why is this interface not correctly released when the method is exited?
我当前的代码如下所示:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Winapi.Windows,
System.Generics.Collections,
System.SysUtils;
type
TForm1 = class
public
Events: TList<TProc>;
constructor Create;
destructor Destroy; override;
end;
TTracingInterfacedObject = class(TInterfacedObject)
public
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
end;
ISharedPtr<T> = interface
['{CC9EE6C5-F07B-40E5-B05D-2DFDBD3404A1}']
function Get: T;
function GetRefCount: Integer;
end;
ICatalog = interface
['{F421BBA8-8DA3-47EE-ADB9-DED26747472E}']
function GetView: ISharedPtr<TForm1>;
property View: ISharedPtr<TForm1> read GetView;
end;
ITree = interface
['{A1E2F71B-124B-48DB-B038-5F90AC5BE94B}']
function GetId: TGUID;
property Id: TGUID read GetId;
end;
TSharedPtr<T: class> = class(TTracingInterfacedObject, ISharedPtr<T>)
private
FObject: T;
public
constructor Create(const AObject: T);
destructor Destroy; override;
function GetRefCount: Integer;
function Get: T;
end;
TCatalog = class(TTracingInterfacedObject, ICatalog)
private
FView: ISharedPtr<TForm1>;
public
constructor Create;
function GetView: ISharedPtr<TForm1>;
end;
TTree = class(TTracingInterfacedObject, ITree)
private
FView: ISharedPtr<TForm1>;
public
constructor Create(const AView: ISharedPtr<TForm1>);
function GetId: TGUID;
end;
function TTracingInterfacedObject._AddRef: Integer;
begin
OutputDebugString(PChar(ClassName + '._AddRef'));
Result := inherited _AddRef;
end;
function TTracingInterfacedObject._Release: Integer;
begin
OutputDebugString(PChar(ClassName + '._Release'));
Result := inherited _Release;
end;
constructor TForm1.Create;
begin
inherited;
Events := TList<TProc>.Create;
end;
destructor TForm1.Destroy;
begin
Events.Free;
inherited;
end;
constructor TSharedPtr<T>.Create(const AObject: T);
begin
inherited Create;
FObject := AObject;
end;
destructor TSharedPtr<T>.Destroy;
begin
FObject.Free;
inherited;
end;
function TSharedPtr<T>.Get: T;
begin
Result := FObject;
end;
function TSharedPtr<T>.GetRefCount: Integer;
begin
Result := FRefCount;
end;
constructor TCatalog.Create;
begin
inherited Create;
FView := TSharedPtr<TForm1>.Create(TForm1.Create) as ISharedPtr<TForm1>;
end;
function TCatalog.GetView: ISharedPtr<TForm1>;
begin
Result := FView;
end;
constructor TTree.Create(const AView: ISharedPtr<TForm1>);
begin
inherited Create;
FView := AView;
end;
function TTree.GetId: TGUID;
begin
Result := TGUID.Empty;
end;
procedure Main;
var
Catalog: ICatalog;
Tree: ITree;
Func: TFunc<TGUID>;
Events: TList<TProc>;
Event: TProc;
begin
Catalog := TCatalog.Create as ICatalog;
Events := Catalog.View.Get.Events;
Event := procedure
begin
end;
Events.Add(Event);
Tree := TTree.Create(Catalog.View) as ITree;
Func := function: TGUID
begin
Result := Tree.Id;
end;
end;
begin
Main;
end.
我在应用程序的最后 end.
处设置了一个断点。
当时的事件日志如下所示:
Debug Output: TSharedPtr<Project1.TForm1>._AddRef Process Project1.exe (3456)
Debug Output: TCatalog._AddRef Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._AddRef Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._AddRef Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._AddRef Process Project1.exe (3456)
Debug Output: TTree._AddRef Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._Release Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._Release Process Project1.exe (3456)
Debug Output: TCatalog._Release Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._Release Process Project1.exe (3456)
Source Breakpoint at [=11=]47F675: C:\Users\Admin\Documents\Embarcadero\Studio\Projects\ViewFail\Project1.dpr line 168. Process Project1.exe (3456)
所以:
- 目录是 _AddRefed 一次和 _Released 一次,这很好。
- 树被 _AddRefed 一次,从未被 _Released,这不是我所期望的。
- Catalog.View 被 _AddRefed 四次,_Released 仅三次,这也不是我所期望的。
为什么会这样?有没有我遗漏的参考循环?
是的,你的代码中有一个循环引用。它是通过匿名方法变量捕获机制创建的。
匿名方法由编译器生成的引用计数支持 class。匿名方法捕获的任何变量都作为字段存储在同一个 class 中。编译器实例化那个 class 的实例,只要匿名方法在范围内就让它保持活动状态。
现在,以上事实不足以形成循环。但是相同的实例(相同的class)将用于备份某些例程中的所有匿名方法。
翻译成您的代码:
TForm1
持有 Events
Catalog
持有 TForm1
Tree
持有 TForm1
那里没有循环 - Tree
没有引用 Catalog
,Catalog
也没有引用 Tree
但是,当您查看您的 Main
过程时,事情就变了。
Main
中的匿名方法将由隐藏的对象实例支持 - 让我们看看那里会有什么:
- 第一个匿名方法
- 第二种匿名方法
Tree
- 由第二个匿名方法捕获
仍然没有可见的循环 - 但是,然后您将第一个匿名方法 Event
添加到 Tree
持有的 Events
列表中。为了使该方法保持活动状态,整个支持对象也将保持活动状态。
Anonymous method object -> Event
-> Tree -> Events -> Event -> Anonymous method object
要打破这个循环,您必须清除一些引用。例如,在 Main
.
的某处将 Tree
设置为 nil
我当前的代码如下所示:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Winapi.Windows,
System.Generics.Collections,
System.SysUtils;
type
TForm1 = class
public
Events: TList<TProc>;
constructor Create;
destructor Destroy; override;
end;
TTracingInterfacedObject = class(TInterfacedObject)
public
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
end;
ISharedPtr<T> = interface
['{CC9EE6C5-F07B-40E5-B05D-2DFDBD3404A1}']
function Get: T;
function GetRefCount: Integer;
end;
ICatalog = interface
['{F421BBA8-8DA3-47EE-ADB9-DED26747472E}']
function GetView: ISharedPtr<TForm1>;
property View: ISharedPtr<TForm1> read GetView;
end;
ITree = interface
['{A1E2F71B-124B-48DB-B038-5F90AC5BE94B}']
function GetId: TGUID;
property Id: TGUID read GetId;
end;
TSharedPtr<T: class> = class(TTracingInterfacedObject, ISharedPtr<T>)
private
FObject: T;
public
constructor Create(const AObject: T);
destructor Destroy; override;
function GetRefCount: Integer;
function Get: T;
end;
TCatalog = class(TTracingInterfacedObject, ICatalog)
private
FView: ISharedPtr<TForm1>;
public
constructor Create;
function GetView: ISharedPtr<TForm1>;
end;
TTree = class(TTracingInterfacedObject, ITree)
private
FView: ISharedPtr<TForm1>;
public
constructor Create(const AView: ISharedPtr<TForm1>);
function GetId: TGUID;
end;
function TTracingInterfacedObject._AddRef: Integer;
begin
OutputDebugString(PChar(ClassName + '._AddRef'));
Result := inherited _AddRef;
end;
function TTracingInterfacedObject._Release: Integer;
begin
OutputDebugString(PChar(ClassName + '._Release'));
Result := inherited _Release;
end;
constructor TForm1.Create;
begin
inherited;
Events := TList<TProc>.Create;
end;
destructor TForm1.Destroy;
begin
Events.Free;
inherited;
end;
constructor TSharedPtr<T>.Create(const AObject: T);
begin
inherited Create;
FObject := AObject;
end;
destructor TSharedPtr<T>.Destroy;
begin
FObject.Free;
inherited;
end;
function TSharedPtr<T>.Get: T;
begin
Result := FObject;
end;
function TSharedPtr<T>.GetRefCount: Integer;
begin
Result := FRefCount;
end;
constructor TCatalog.Create;
begin
inherited Create;
FView := TSharedPtr<TForm1>.Create(TForm1.Create) as ISharedPtr<TForm1>;
end;
function TCatalog.GetView: ISharedPtr<TForm1>;
begin
Result := FView;
end;
constructor TTree.Create(const AView: ISharedPtr<TForm1>);
begin
inherited Create;
FView := AView;
end;
function TTree.GetId: TGUID;
begin
Result := TGUID.Empty;
end;
procedure Main;
var
Catalog: ICatalog;
Tree: ITree;
Func: TFunc<TGUID>;
Events: TList<TProc>;
Event: TProc;
begin
Catalog := TCatalog.Create as ICatalog;
Events := Catalog.View.Get.Events;
Event := procedure
begin
end;
Events.Add(Event);
Tree := TTree.Create(Catalog.View) as ITree;
Func := function: TGUID
begin
Result := Tree.Id;
end;
end;
begin
Main;
end.
我在应用程序的最后 end.
处设置了一个断点。
当时的事件日志如下所示:
Debug Output: TSharedPtr<Project1.TForm1>._AddRef Process Project1.exe (3456)
Debug Output: TCatalog._AddRef Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._AddRef Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._AddRef Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._AddRef Process Project1.exe (3456)
Debug Output: TTree._AddRef Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._Release Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._Release Process Project1.exe (3456)
Debug Output: TCatalog._Release Process Project1.exe (3456)
Debug Output: TSharedPtr<Project1.TForm1>._Release Process Project1.exe (3456)
Source Breakpoint at [=11=]47F675: C:\Users\Admin\Documents\Embarcadero\Studio\Projects\ViewFail\Project1.dpr line 168. Process Project1.exe (3456)
所以:
- 目录是 _AddRefed 一次和 _Released 一次,这很好。
- 树被 _AddRefed 一次,从未被 _Released,这不是我所期望的。
- Catalog.View 被 _AddRefed 四次,_Released 仅三次,这也不是我所期望的。
为什么会这样?有没有我遗漏的参考循环?
是的,你的代码中有一个循环引用。它是通过匿名方法变量捕获机制创建的。
匿名方法由编译器生成的引用计数支持 class。匿名方法捕获的任何变量都作为字段存储在同一个 class 中。编译器实例化那个 class 的实例,只要匿名方法在范围内就让它保持活动状态。
现在,以上事实不足以形成循环。但是相同的实例(相同的class)将用于备份某些例程中的所有匿名方法。
翻译成您的代码:
TForm1
持有Events
Catalog
持有TForm1
Tree
持有TForm1
那里没有循环 - Tree
没有引用 Catalog
,Catalog
也没有引用 Tree
但是,当您查看您的 Main
过程时,事情就变了。
Main
中的匿名方法将由隐藏的对象实例支持 - 让我们看看那里会有什么:
- 第一个匿名方法
- 第二种匿名方法
Tree
- 由第二个匿名方法捕获
仍然没有可见的循环 - 但是,然后您将第一个匿名方法 Event
添加到 Tree
持有的 Events
列表中。为了使该方法保持活动状态,整个支持对象也将保持活动状态。
Anonymous method object -> Event
-> Tree -> Events -> Event -> Anonymous method object
要打破这个循环,您必须清除一些引用。例如,在 Main
.
Tree
设置为 nil