“弱参考”:需要地下解释 [英] "Weak reference": down to earth explanation needed

查看:116
本文介绍了“弱参考”:需要地下解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在Delphi中提供弱引用的解释吗?



我注意到这个概念在某些库/框架中经常被提及源代码我仔细检查我很沮丧,想要清楚地了解它。

解决方案

通过接口引用引用的实例保持对方在基于引用计数的接口实现中。



弱引用用于打破保持彼此活着熊拥抱。这是通过将一个引用声明为纯指针来规避引用计数机制来实现的。

  IFriend = Interface(IInterface)
结束

TFriend = class(TInterfacedObject,IFriend)
private
FFriend:IFriend;
结束


var
Peter:IFriend;
约翰:IFriend;
begin
Peter:= TFriend.Create;
John:= TFriend.Create;

Peter.Friend:= John;
John.Friend:= Peter;
结束

即使彼得和约翰超出范围,他们的实例仍然保留,因为他们的相互参考保留他们的



该问题在复合模式(父 - 子关系)中更常见,其中该子对父对象的引用:

  ISomething = Interface(IInterface)
end;

TSomething = class(TInterfacedObject,ISomething)
end;

TParent = class(TSomething)
FChildren:TInterfacedList;
结束

TChild = class(TSomething)
FParent:ISomething;
结束

同样,父母和孩子可以保持彼此相邻,因为他们的相互参考保持他们的引用计数不到零。



这是通过弱引用解决的:

  TChild = class(TSomething)
FParent:Pointer;
结束

通过将FParent声明为纯指针,引用计数机制不会起作用后面引用父母。当父母超出范围时,其引用计数现在可以下降到零,因为它的孩子不再保持其引用计数高于零。



注意这个解决方案需要仔细注意终身管理。在这些班级的外部上的某些东西保留对孩子的提及时,儿童可以在父母的生命之后活着。而当孩子承担父参考总是指向一个有效的实例时,这可以导致各种有趣的AV。如果您需要,请确保当父母超出范围时,它会使孩子在其引用自己的孩子之前没有返回引用。


Can someone provide an explanation of a weak reference in Delphi?

I noticed that the concept is often mentioned in some library/framework source code I scrutinize. I'm in a limbo and want to have a clear cut understanding of it.

解决方案

Instances that reference each other by interface references keep each other alive in a reference count based interface implementation.

A weak reference is used to break the "keep each other alive" bear hug. This is done by declaring one reference as a pure pointer to circumvent the reference counting mechanism.

IFriend = Interface(IInterface)
end;

TFriend = class(TInterfacedObject, IFriend)
private
  FFriend: IFriend;
end;


var
  Peter: IFriend;
  John: IFriend;
begin
  Peter := TFriend.Create;
  John := TFriend.Create;

  Peter.Friend := John;
  John.Friend := Peter;
end;

Even when Peter and John go out of scope, their instances are kept around because their mutual reference keeps their refcount from dropping to zero.

The problem is more commonly found in composite patterns (parent - child relationships) where the child has a back reference to the parent:

ISomething = Interface(IInterface)
end;

TSomething = class(TInterfacedObject, ISomething)
end;

TParent = class(TSomething)
  FChildren: TInterfacedList;
end;

TChild = class(TSomething)
  FParent: ISomething;
end;

Again, parent and child can keep eachother around because their mutual reference keeps their refcount from dropping to zero.

This is solved with a weak reference:

TChild = class(TSomething)
  FParent: Pointer;
end;

By declaring the FParent as a "pure" pointer the reference counting mechanism doesn't come into play for the back reference to the parent. When a parent goes out of scope, its reference count now can drop to zero because its children no longer keep its ref count above zero.

Note This solution does require careful attention to lifetime management. Children can be kept alive beyond the life time of the parent when something on the "outside" of these classes keeps a reference to a child. And this can lead to all sorts of interesting AV's when the child assumes the parent reference always points to a valid instance. If you need it, make sure that when the parent goes out of scope, it makes the children nil their back references before it nils its own references to its children.

这篇关于“弱参考”:需要地下解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆