shared_ptr的周期性依赖关系问题是什么? [英] What is the cyclic dependency issue with shared_ptr?

查看:137
本文介绍了shared_ptr的周期性依赖关系问题是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了共享指针,并了解了如何使用.但是我从来没有理解共享指针的循环依赖问题以及弱指针如何解决这些问题.有人可以清楚地解释这个问题吗?

I read about shared pointers and understood how to use. But I never understood the cyclic dependency problem with shared pointers and how weak pointers are going to fix those issues. Can any one please explain this problem clearly?

推荐答案

问题并不复杂.让-->表示共享指针:

The problem isn't that complex. Let --> represent a shared pointer:

The rest of the program  --> object A --> object B
                                    ^     |
                                     \    |
                                      \   v
                                        object C

因此,我们已经有了带有共享指针的循环依赖项.每个对象的引用计数是多少?

So we've got ourselves a circular dependency with shared pointers. What's the reference count of each object?

A:  2
B:  1
C:  1

现在假定程序的其余部分(或至少包含共享指向A的指针的部分)被破坏了.然后将A的引用计数减少1,因此循环中每个对象的引用计数为1.那么删除了什么?没有.但是我们要删除什么?一切,因为程序的其余部分再也无法访问我们的对象了.

Now suppose the rest of the program (or at any rate the part of it that holds a shared pointer to A) is destroyed. Then the refcount of A is reduced by 1, so the reference count of each object in the cycle is 1. So what gets deleted? Nothing. But what do we want to be deleted? Everything, because none of our objects can be reached from the rest of the program any more.

因此,在这种情况下,解决方法是将C到A的链接更改为弱指针.弱指针不会影响其目标的引用计数,这意味着当程序的其余部分释放A时,其refcount会变为0.因此将其删除,因此将B删除,将C删除.

So the fix in this case is to change the link from C to A into a weak pointer. A weak pointer doesn't affect the reference count of its target, which means that when the rest of the program releases A, its refcount hits 0. So it's deleted, hence so is B, hence so is C.

但是,在程序的其余部分释放A之前,C可以通过锁定弱指针随时访问A.只要C主动对A进行处理,这就会将其提升为共享指针(并将A的引用计数增加到2).这意味着,如果在执行此操作的同时释放A,则其引用计数将仅下降为1. C中使用A的代码不会崩溃,并且只要该短期共享指针被破坏,A就会被删除.在锁定弱指针的代码块的末尾.

Before the rest of the program releases A, though, C can access A whenever it likes by locking the weak pointer. This promotes it to a shared pointer (and increases the refcount of A to 2) for as long as C is actively doing stuff with A. That means if A is otherwise released while this is going on then its refcount only falls to 1. The code in C that uses A doesn't crash, and A is deleted whenever that short-term shared pointer is destroyed. Which is at the end of the block of code that locked the weak pointer.

通常,确定弱指针可能的放置位置很复杂.在循环中的对象之间需要某种不对称性,以便选择破坏它的位置.在这种情况下,我们知道A是程序其余部分所引用的对象,因此我们知道打破循环的地方就是指向A的任何地方.

In general, deciding where to put the weak pointers might be complex. You need some kind of asymmetry among the objects in the cycle in order to choose the place to break it. In this case we know that A is the object referred to by the rest of the program, so we know that the place to break the cycle is whatever points to A.

这篇关于shared_ptr的周期性依赖关系问题是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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