cle_details"
class="tags" href="/tags/CLASS.html" title=
class>
class="details">
class="tags" href="/tags/CLASS.html" title=
class>
class="arti
cle_
content">
循环引用:
引用计数是一种便利的内存管理机制c;但它有一个很大的缺点c;那就是不能管理循环引用的对象。一个简单的例子如下:
color:blue">#include color:#a31515"><string>
color:blue">#include color:#a31515"><iostream>
color:blue">#include color:#a31515"><boost/shared_ptr.hpp>
color:blue">#include color:#a31515"><boost/weak_ptr.hpp>
color:blue">class="tags" href="/tags/CLASS.html" title=class>class parent;
color:blue">class="tags" href="/tags/CLASS.html" title=class>class children;
color:blue">typedef boost::shared_ptr<parent> parent_ptr;
color:blue">typedef boost::shared_ptr<children> children_ptr;
color:blue">class="tags" href="/tags/CLASS.html" title=class>class parent
{
color:blue">public:
~parent() { std::cout <<color:#a31515">"destroying parent/n"; }
color:blue">public:
children_ptr children;
};
color:blue">class="tags" href="/tags/CLASS.html" title=class>class children
{
color:blue">public:
~children() { std::cout <<color:#a31515">"destroying children/n"; }
color:blue">public:
parent_ptr parent;
};
color:blue">void test()
{
parent_ptr father(color:blue">new parent());
children_ptr son(color:blue">new children);
father->children = son;
son->parent = father;
}
color:blue">void main()
{
std::cout<<color:#a31515">"begin test.../n";
test();
std::cout<<color:#a31515">"end test./n";
}
运行该程序可以看到c;即使退出了test函数后c;由于parent和children对象互相引用c;它们的引用计数都是1c;不能自动释放c;并且此时这两个对象再无法访问到。这就引起了c++中那臭名昭著的color:#c0504d">内存泄漏。
一般来讲c;解除这种循环引用有下面有三种可行的方法:
-
当只剩下最后一个引用的时候需要手动打破循环引用释放对象。
-
当parent的生存期超过
children的生存期的时候
c;
children改为使用一个普通指针指向parent。
-
使用弱引用的智能指针打破这种循环引用。
虽然这三种方法都可行c;但方法1和方法2都需要程序员手动控制c;麻烦且容易出错。这里主要介绍一下第三种方法和boost中的弱引用的智能指针boost::weak_ptr。
强引用和弱引用
一个强引用当被引用的对象活着的话c;这个引用也存在(就是说c;当至少有一个强引用c;那么这个对象就不能被释放)。boost::share_ptr就是强引用。
相对而言c;弱引用当引用的对象活着的时候不一定存在。仅仅是当它存在的时候的一个引用。弱引用并不修改该对象的引用计数c;这意味这弱引用它并不对对象的内存进行管理c;在功能上类似于普通指针c;然而一个比较大的区别是c;弱引用能检测到所管理的对象是否已经被释放c;从而避免访问非法内存。
boost::weak_ptr
boost::weak_ptr<T>是boost提供的一个弱引用的智能指针c;它的声明可以简化如下:
color:blue">namespace boost {
color:blue">template<color:blue">typename T> color:blue">class="tags" href="/tags/CLASS.html" title=class>class weak_ptr {
color:blue">public:
color:blue">template <color:blue">typename Y>
weak_ptr(color:blue">const shared_ptr<Y>& r);
weak_ptr(color:blue">const weak_ptr& r);
~weak_ptr();
T* get() color:blue">const;
color:blue">bool expired() color:blue">const;
shared_ptr<T> lock() color:blue">const;
};
}
可以看到c;boost::weak_ptr必须从一个boost::share_ptr或另一个boost::weak_ptr转换而来c;这也说明c;进行该对象的内存管理的是那个强引用的boost::share_ptr。boost::weak_ptr只是提供了对管理对象的一个访问手段。
boost::weak_ptr除了对所管理对象的基本访问功能(通过get()函数)外c;还有两个常用的功能函数:expired()用于检测所管理的对象是否已经释放;lock()用于获取所管理的对象的强引用指针。
通过boost::weak_ptr来打破循环引用
由于弱引用不更改引用计数c;类似普通指针c;只要把循环引用的一方使用弱引用c;即可解除循环引用。对于上面的那个例子来说c;只要把children的定义改为如下方式c;即可解除循环引用:
color:blue">class="tags" href="/tags/CLASS.html" title=class>class children
{
color:blue">public:
~children() { std::cout <<color:#a31515">"destroying children/n"; }
color:blue">public:
boost::weak_ptr<parent> parent;
};
最后值得一提的是,虽然通过弱引用指针可以有效的解除循环引用c;但这种方式必须在程序员能预见会出现循环引用的情况下才能使用c;也可以是说这个仅仅是一种编译期的解决方案c;如果程序在运行过程中出现了循环引用c;还是会造成内存泄漏的。因此c;不要认为只要使用了智能指针便能杜绝内存泄漏。毕竟c;对于C++来说c;由于没有垃圾回收机制c;内存泄漏对每一个程序员来说都是一个非常头痛的问题。