strong reference and weak reference
2011-04-12 22:11
330 查看
Cyclic References
Reference counting is a convenient resource management mechanism, it has one fundamental drawback though: cyclic references are not freed automatically, and are hard to detect by the computer. The simplest example is this:struct CDad; struct CChild; typedef boost::shared_ptr< CDad> CDadPtr; typedef boost::shared_ptr< CChild> CChildPtr; struct CDad : public CSample { CChildPtr myBoy; }; struct CChild : public CSample { CDadPtr myDad; }; // a "thing" that holds a smart pointer to another "thing": CDadPtr dad(new CDad); CChildPtr child(new CChild); // deliberately create a circular reference: dad-> myBoy = child; child-> myDad = dad; // resetting one ptr... child.reset();
dadstill references the
CDadobject, which itself references the
CChild. The whole thing looks like this:
If we now call
dad.reset(), we lose all "contact" with the two objects. But this leaves both with exactly one reference, and the shared pointers see no reason to delete either of them! We have no access to them anymore, but they mutually keep
themselves "alive". This is a memory leak at best; in the worst case, the objects hold even more critical resources that are not released correctly.
The problem is not solvable with a "better" shared pointer implementation (or at least, only with unacceptable overhead and restrictions). So you have to break that cycle. There are two ways:
Manually break the cycle before you release your last reference to it
When the lifetime of
Dadis known to exceed the lifetime of
Child, the child can use a normal (raw) pointer to
Dad.
Use a
boost::weak_ptrto break the cycle.
Solutions (1) and (2) are no perfect solutions, but they work with smart pointer libraries that do not offer a
weak_ptrlike boost does. But let's look at
weak_ptrin detail:
Using weak_ptr to break cycles
Strong vs. Weak References:A strong reference keeps the referenced object alive (i.e., as long as there is at least one strong reference to the object, it is not deleted).
boost::shared_ptracts as a strong reference. In contrast, a weak reference does not keep the object alive, it merely references it as long as it lives.
Note that a raw C++ pointer in this sense is a weak reference. However, if you have just the pointer, you have no ability to detect whether the object still lives.
boost::weak_ptr<T>is a smart pointer acting as weak reference. When you need it, you can request a strong (shared) pointer from it. (This can be
NULLif the object was already deleted.) Of course, the strong pointer should be released immediately after use. In the above sample, we can decide to make one pointer weak:
struct CBetterChild : public CSample { weak_ptr< CDad> myDad; void BringBeer() { shared_ptr< CDad> strongDad = myDad.lock(); // request a strong pointer if (strongDad) // is the object still alive? strongDad-> SetBeer(); // strongDad is released when it goes out of scope. // the object retains the weak pointer } };
See the Sample 5 for more.
reference: http://www.codeproject.com/KB/stl/boostsmartptr.aspx#Cyclic%20References
相关文章推荐
- Block, weakSelf and strongSelf
- x264 编码器选项分析 (x264 Codec Strong and Weak Points) 2
- x264 编码器选项分析 (x264 Codec Strong and Weak Points) 1
- x264 编码器选项分析 (x264 Codec Strong and Weak Points) 1
- WeakReference vs SoftReference vs PhantomReference vs StrongReference
- What the difference between __weak and __block reference?
- Java引用--StrongReference、SoftReference、WeakReference、PhantomReference
- [置顶] (柯昌合)内存优化之"软引用" Strong, Soft, Weak, Phantom Reference(柯昌合)
- java的4种reference: strong, soft, weak, phantom
- WeakReference and WeakHashMap
- XCode4.2下的物件參照 weak and strong
- Java:WeakReference vs SoftReference vs PhantomReference vs Strong reference
- XCode4.2下的物件參照 weak and strong
- WeakEvent and WeakReference
- java WeakReference SoftReference and PhatomReference 介绍
- StrongReference、SoftReference、WeakReference、PhantomReference
- Java(强、软、弱、虚)引用总结--StrongReference、SoftReference、WeakReference、PhantomReference
- Reference Soft Strong Weak
- java的4种reference: strong, soft, weak, phantom
- Java引用总结--StrongReference、SoftReference、WeakReference、PhantomReference