strong reference and weak reference
来源:互联网 发布:多益网络福利待遇 编辑:程序博客网 时间:2024/05/16 01:50
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();
dad
still references the CDad
object, which itself references theCChild
. 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
Dad
is known to exceed the lifetime ofChild
, the child can use a normal (raw) pointer toDad
. - Use a
boost::weak_ptr
to break the cycle.
Solutions (1) and (2) are no perfect solutions, but they work with smart pointer libraries that do not offer aweak_ptr
like boost does. But let's look at weak_ptr
in 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_ptr
acts 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 beNULL
if 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
- strong reference and weak reference
- Reference Soft Strong Weak
- Strong, Soft, Weak, Phantom Reference
- IBOutlet的weak和strong reference
- strong/soft/weak/phantom reference比较
- Weak Reference
- java的4种reference: strong, soft, weak, phantom
- java的4种reference: strong, soft, weak, phantom
- 内存优化之"软引用" Strong, Soft, Weak, Phantom Reference
- java持有引用(Strong/soft/weak/phantom Reference)
- 强引用(Strong Reference)/软引用(Soft Reference)/弱引用(Weak Reference)/虚引用(Phantom Reference)
- AS3 weak reference & GC
- weak reference - 1
- weak reference的介绍
- weak reference的理解
- [置顶] (柯昌合)内存优化之"软引用" Strong, Soft, Weak, Phantom Reference(柯昌合)
- [Java] weak reference 和 soft reference
- What is the difference between a soft reference and a weak reference in Java?
- GDI+关于画图的一些问题
- 贪吃蛇游戏的简单设计
- flex与c#基于socket的实时互动网络游戏编程教程一
- 创建表事报错:ORA-01950: no privileges on tablespace 'tablespace_name‘ 解决
- VS编译程序无法定位程序输入点的问题
- strong reference and weak reference
- “堆被损坏”这个让人崩溃的问题
- 4月12日,安徽电子商务产业园授牌仪式、安徽省电子商务门户网站(安徽买卖提)开通暨签约仪式在安徽电子商务产业园隆重举行。
- 一个stream试验-pre_instantiation_setup/post_instantiation_setup 模仿piner的全库stream试验
- string , String, StringBuilder的聯繫以及區別
- 二分查找算法
- #include
- 写代码的一些准则
- Acm hdu1052 田忌赛马