如何使用 std::shared_ptr 检测或避免循环引用?

发布于 2025-01-08 18:40:40 字数 77 浏览 0 评论 0原文

我知道有 weak_ptr 可以打破循环,但在发现问题后这是一个修复。是否有可用于检测或避免循环引用的模式或工具?

I know that there is weak_ptr to break the cycle, but that is a fix, after the problem is discovered. Is there a pattern or tool that can be used to either detect or avoid cyclic referencing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薔薇婲 2025-01-15 18:40:40

您可以通过设计避免这种情况。正如 Stephan T. Lavavej 在 GoingNative2012 会议上指出的那样(您可以在线查看视频),“所有权”是一个有向无环图,即 DAG。 DAG 中没有循环。如果你的所有权图不是 DAG,那么你的设计是……有问题的,因为 A 拥有 B,B 拥有 A 是没有意义的。但shared_ptr是一个“共享所有权指针”。持有此类指针的对象或范围拥有指针对象。尝试从所有权图的角度思考。

shared_ptr 并不是适合所有情况的工具。它不应该允许您像在 Java 中那样进行编码,而不必(太多)考虑所有权。它应该提供自动和确定性的清理。如果您需要一个“非拥有”指针,weak_ptr 或原始指针是合适的。只需确保原始指针指向的对象保持活动状态足够长的时间即可。

You avoid this by design. As Stephan T. Lavavej pointed out so nicely at the GoingNative2012 conference (you can check out the videos online), "ownership" is a directed acyclic graph, a DAG. There are no cycles in a DAG. If your ownership graph is not a DAG, your design is … questionable because A owning B and B owning A makes no sense. But shared_ptr is a "shared ownership pointer". The object or scope holding such a pointer owns the pointee. Try to think in terms of ownership graphs.

shared_ptr is not the right tool for every case. It's not supposed to allow you to code just like you'd do it in, say, Java where you don't have to think about ownership (much). It's supposed to provide automatic and deterministic cleanup. If you need a "non-owning" pointer, weak_ptr or a raw pointer is appropriate. Just make sure that the object a raw pointer points to stays alive long enough.

鹿童谣 2025-01-15 18:40:40

显而易见的答案是不要对以下对象使用 shared_ptr
它们本身可能包含一个shared_ptrshared_ptr 有点
特殊,应谨慎使用。

The obvious answer is not to use shared_ptr to objects which
themselves might contain a shared_ptr. shared_ptr is somewhat
special, and should be used sparingly.

梦中的蝴蝶 2025-01-15 18:40:40

我强烈赞同 Selibitze 所说的并重新考虑设计。如果你真正拥有的是单向所有权并且只是在相反方向观察,请考虑weak_ptr。这允许您检查一个对象是否处于活动状态,但不会仅仅因为您有一个指向它的指针而使该对象保持活动状态。

I would strongly echo what sellibitze said and rethink the design. If what you really have is a one way ownership and simply observing in the opposite direction consider weak_ptr. This allows you to check if an object is alive, but does not keep the object alive simply because you have a pointer to it.

心凉 2025-01-15 18:40:40

您可以实现一个,类似于 smart_ptr 的包装器,它在创建时存储 this 指针(例如通过宏)。然后构建有向图,其边从存储的 thisshared_ptr 包含的对象,并检测任何循环,例如通过拓扑排序。

我建议仅针对具有密集 shared_ptr 使用且无法控制所有设计方面的大型代码库。对于其他情况,只需使用@sellibitze 推荐。

You can implement one, something like a wrapper over smart_ptr which on creation stores this pointer (e.g. by macro). Then build directed graph with edges from stored this to object contained by shared_ptr and detect any cycles, e.g. by topological sorting.

I would recommend this only for large code base with intensive shared_ptr usage and inability to control all design aspects. For other cases just use @sellibitze recommendation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文