什么是“根引用”?
引用自 ( 在 C# 中安全,但在C++,简单返回指针/引用, 答案 3),作者:Eric lippert。
另外,请注意,并不是对 Person 对象的任何引用使其保持活动状态。引用必须是 root 的。您可以有两个相互引用但无法访问的 Person 对象;事实上,每个人都有一个参考并不能让他们保持活力;其中一个参考文献必须是 root 的。
我不明白,有人可以解释一下什么是根引用吗?
Quote from ( Safe in C# not in C++, simple return of pointer / reference, answer 3) by Eric lippert.
Also, note that it is not any reference to the Person object that keeps it alive. The reference has to be rooted. You could have two Person objects that reference each other but are otherwise unreachable; the fact that each has a reference does not keep them alive; one of the references has to be rooted.
I dont understand, can someone explain what a rooted reference is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这意味着GC根。
阅读这篇文章,也许它会帮助你理解:
(强调我的)
It means a GC root.
Have a read through this article, maybe it will help with your understanding:
(emphasis mine)
有各种各样的根对象,例如 CLR 内部对象、元数据对象等。这个 帖子可能有帮助:
There're all kinds of root objects, like CLR internal objects, metadata objects, etc. This post may help:
根是一个存储位置,例如局部变量,它可以包含引用并且已知已被初始化,并且您的程序在将来的某个时刻使用它而无需通过其他对象引用。
在 Ian Griffiths 的Programming C# 10.0中第七章中指出:
另外本文档 据微软称:
根据 Joseph Albahari 的 C# 12 in a Nutshell,根 根是使对象保持活动状态的东西。如果一个对象没有被根直接或间接引用,那么它将有资格进行垃圾回收。
下图来自同一本书,可以帮助理解:
A root is a storage location, such as local variable, that could contain a reference and is known to have been initialized, and that your program use at some point in the future without needing to go via some other object reference.
In Programming C# 10.0 by Ian Griffiths in the seventh chapter it is stated that:
additionally in this document by Microsoft it is said that:
According to C# 12 in a Nutshell by Joseph Albahari, Root A root is something that keeps an object alive. If an object is not directly or indirectly referenced by a root, it will be eligible for garbage collection.
The figure below from the same book can help in understanding: