boost::shared_ptr的内存管理机制:
boost::shared_ptr的管理机制其实并不复杂,就是对所管理的对象进行了引用计数,当新增一个boost::shared_ptr对该对象进行管理时,就将该对象的引用计数加一;减少一个boost::shared_ptr对该对象进行管理时,就将该对象的引用计数减一,如果该对象的引用计数为0的时候,说明没有任何指针对其管理,才调用delete释放其所占的内存。
上面的那个例子可以的图示如下:
1.
sp1对implementation对象进行管理,其引用计数为1
2.
增加sp2对implementation对象进行管理,其引用计数增加为2
3.
sp1释放对implementation对象进行管理,其引用计数变为1
4.
sp2释放对implementation对象进行管理,其引用计数变为0,该对象被自动删除
boost::shared_ptr的特点:
和前面介绍的boost::scoped_ptr相比,boost::shared_ptr可以共享对象的所有权,因此其使用范围基本上没有什么限制(还是有一些需要遵循的使用规则,下文中介绍),自然也可以使用在stl的容器中。另外它还是线程安全的,这点在多线程程序中也非常重要。
boost::shared_ptr的使用规则:
boost::shared_ptr并不是绝对安全,下面几条规则能使我们更加安全的使用boost::shared_ptr:
1.
避免对shared_ptr所管理的对象的直接内存管理操作,以免造成该对象的重释放
2.
shared_ptr并不能对循环引用的对象内存自动管理(这点是其它各种引用计数管理内存方式的通病)。
3.
不要构造一个临时的shared_ptr作为函数的参数。
如下列代码则可能导致内存泄漏:
void test()
{
foo(boost::shared_ptr<implementation>(new implementation()),g());
}
正确的用法为:
void test()
{
boost::shared_ptr<implementation> sp (new implementation());
foo(sp,g());
}
下面是实例:
#include <boost/enable_shared_from_this.hpp> #include <boost/smart_ptr.hpp> #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/smart_ptr/enable_shared_from_this.hpp> #include <iostream> using namespace std; class test_shared_ptr : public boost::enable_shared_from_this<test_shared_ptr> { public: test_shared_ptr(int n) : x(n) { }; void print() { cout << "value : " << x << endl; }; int x; }; void test001() { test_shared_ptr *a = new test_shared_ptr(20); boost::shared_ptr<test_shared_ptr> sp(a); boost::shared_ptr<test_shared_ptr> p = a->shared_from_this(); //这里使用raw ptr 构造出来的 shared_ptr p->print(); // value: 20 cout << p.use_count() << endl; // 2 } void test002() { boost::shared_ptr<test_shared_ptr> sp = boost::make_shared<test_shared_ptr>(10); boost::shared_ptr<test_shared_ptr> p = sp->shared_from_this(); // 这里使用shared_ptr 构造出shared_ptr 指针 p->print(); cout << p.use_count() << endl; } void test003() { test_shared_ptr *a = new test_shared_ptr(10); boost::shared_ptr<test_shared_ptr> sp(a); //没有这句会出错 boost::shared_ptr<test_shared_ptr> p = a->shared_from_this(); p->print(); cout << p.use_count() << endl; } void test004() { test_shared_ptr ss(30); //声明 不使用指针, 在栈上分配内存 如果使用指针就在堆上分配内存,这里使用错误 //智能指针就是方便管理堆上的内存的,不要管理栈上的内存 boost::shared_ptr<test_shared_ptr> p = ss.shared_from_this(); p->print(); cout << p.use_count() << endl; } class implementation { public: ~implementation() { std::cout <<"destroying implementation\n"; } void do_something() { std::cout << "did something\n"; } }; void test_implementation() { boost::shared_ptr<implementation> sp1(new implementation()); sp1->do_something(); std::cout<<"The Sample now has "<<sp1.use_count()<<" references\n"; boost::shared_ptr<implementation> sp2 = sp1; std::cout<<"The Sample now has "<<sp2.use_count()<<" references\n"; sp1.reset(); std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<<" references\n"; sp2.reset(); std::cout<<"After Reset sp2.The Sample now has "<<sp2.use_count()<<" references\n"; } int main() { test001(); cout << "test002()" << endl; test002(); test003(); cout << "test004()" << endl; // test004(); test_implementation(); return 0; }