1:
#include <boost/smart_ptr.hpp>
#include <iostream>
#include <string>
using namespace std;
using namespace boost;
int main()
{
int *a = new int(10);
{
scoped_ptr<int> sp (a);
cout<<*sp<<endl; //10
}
cout<<*a<<endl; //8916792
}
注意这里,只有一份拷贝,因此不管是 scoped_ptr 出作用域 或者是 delete a 都会使另外一个指针指向非法的内存区域。
2:
不太建议用scoped_array
不能动态增长;也没有迭代器支持
同样的用法,完全可以用 vector 替代它
3:
我觉得boost::shared_ptr 这里有可能会出问题
delete 野指针 之后 ,shared_ptr 的 use_count并没有减少
但是 其实 share_ptr 并不知道 我什么时候 会 delete 野指针。。。
正确的用法 我想就是 用share_ptr 管理 堆上内存之后 永远不要再使用delete
4:
make_shared 很虎逼的用法。。。
shared_ptr<int> spp = make_shared<int>(10);
assert(spp);
shared_ptr<vector<int> > sv = make_shared<vector<int> >(10,2);
assert(sv->size()==10);
5:
可定制的删除器:
void any_func(int * p)
{
cout<<*p<<endl; // 20
cout<<"some operate"<<endl; //some operate
}
int main()
{
shared_ptr<int> p(new int(20),&any_func);
return 0;
}
6:
shared_from_this
利用weak_ptr 获得 this 指针的shared_ptr 对象使用weak_ptr 观测this 指针 需要的时候调用shared_from_this() 返回一个复合要求的shared_ptr 供外界使用
注意: 这句纠结的话的意思就是说: 如果 有一个 shared_ptr 指向 你的野指针 this 那么就会产生一个weak_ptr 监视指针 这时候野指针就会有
shared_from_this 这个方法可以使用 返回一个指向自己的shared_ptr 指针
先给出正确的用法:
class test_shared_ptr : public enable_shared_from_this<test_shared_ptr>
{
public:
test_shared_ptr(int n):x(n){};
void print(){cout<<"value : "<<x<<endl;};
int x;
};
int main()
{
test_shared_ptr *a = new test_shared_ptr(20);
shared_ptr<test_shared_ptr> sp(a);
shared_ptr<test_shared_ptr> p = a->shared_from_this(); //这里使用raw ptr 构造出来的 shared_ptr
p->print(); // value: 20
cout<<p.use_count()<<endl; // 2
return 0;
}
class test_shared_ptr : public enable_shared_from_this<test_shared_ptr>
{
public:
test_shared_ptr(int n):x(n){};
void print(){cout<<"value : "<<x<<endl;};
int x;
};
int main()
{
shared_ptr<test_shared_ptr> sp = make_shared<test_shared_ptr>(10);
shared_ptr<test_shared_ptr> p =sp->shared_from_this(); // 这里使用shared_ptr 构造出shared_ptr 指针
p->print();
cout<<p.use_count()<<endl;
return 0;
}
下面是错误的用法:
class test_shared_ptr : public enable_shared_from_this<test_shared_ptr>
{
public:
test_shared_ptr(int n):x(n){};
void print(){cout<<"value : "<<x<<endl;};
int x;
};
int main()
{
test_shared_ptr *a = new test_shared_ptr(10);
shared_ptr<test_shared_ptr> p =a->shared_from_this();
p->print();
cout<<p.use_count()<<endl;
return 0;
}
这里是会报错的!
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
what(): tr1::bad_weak_ptr
我曾经遇到过这个问题
try
catch 之后 只报出 tr1::bad_weak_ptr 当时百思不得其解!!! 然后还被陈老板批评了。。。。。。。。。。。。。。。。。。。。。
class test_shared_ptr : public enable_shared_from_this<test_shared_ptr>
{
public:
test_shared_ptr(int n):x(n){};
void print(){cout<<"value : "<<x<<endl;};
int x;
};
int main()
{
test_shared_ptr ss;
shared_ptr<test_shared_ptr> p = ss.shared_from_this();
return 0;
}
这种傻逼的用法我就不说了。。。 本来用智能指针就是方便管理堆上的内存的。。。 你用它管理栈上的内存。。。