以对象管理资源 auto_ptr和shared_ptr
2012-08-26 08:47
316 查看
void doSomething() { int *a=new int(100); /* ...... 如果此处的代码发生异常,doSomething()会提早返回,而delete也将不会执行,最终导致资源泄漏 */ delete a; }
为了保证doSomething()中资源总是能够得以释放,我们将资源放入对象中,当控制流离开doSomething(),对象的析构函数自动释放资源。也就是:把资源放进对象内,我们便可依赖C++的“析构函数自动调用机制”确保资源被释放。所以,修改后的代码可能是这样:
class srcManage { public: srcManage(int _a) { a=new int(_a); } private: int *a; }; void doSomething() { srcManage s(100); /* ...... 无论该函数什么时间返回,一旦控制流离开该函数,s的析构函数便会被调用,以释放资源 */ }
其实,C++标准库提供的auto_ptr和boost库提供的shared_ptr已经这样的功能。
auto_ptr:
#include <iostream> #include <memory> //auto_ptr using namespace std; void doSomething() { //构造函数指定了explicit,所以必须使用初始化的形式 //std::auto_ptr<int> a=new int(100); 这样的形式是错误的 std::auto_ptr<int> a(new int(100)); //get()方法返回原始指针 cout<<*a<<" "<<a.get()<<endl; //重载 * 操作符 *a=200; cout<<*a<<" "<<a.get()<<endl; cout<<"**************************************************"<<endl; /* auto_ptr:通过拷贝构造函数或赋值操作符复制它们,它们会变成NULL,赋值得到的指针取得资源的唯一拥有权 */ std::auto_ptr<int> b(a); cout<<a.get()<<endl;//a变为NULL cout<<*b<<" "<<b.get()<<endl; cout<<"**************************************************"<<endl; /* auto_ptr:通过赋值操作符,如果左操作数绑定了具体对象,那么将会删除左操作数的对象,并且赋值为右操作数 */ std::auto_ptr<int> c(new int(300)); cout<<*c<<" "<<c.get()<<endl; c=b;//b变为NULL,删除原先的c绑定的对象 cout<<b.get()<<endl; cout<<*c<<" "<<c.get()<<endl; /* 当控制流离开doSomething(),智能指针析构时会同时删除指向的对象 */ } int main() { doSomething(); }
shared_ptr:
#include <iostream> #include <boost/shared_ptr.hpp> using namespace std; using namespace boost; /* shared_ptr是引用计数型智能指针,允许多个指针指向同一对象 析构时将计数减一,当指向对象的计数为0时,删除对象。 可以自定义删除器 */ //自定义删除器 void myDelete(int *p) { cout<<"地址是"<<p<<"的数"<<*p<<"的引用计数为0了,执行我自己的删除方法."<<endl; /* ...... 执行自己的删除过程 */ delete p; } void doSharedSomething() { boost::shared_ptr<int> a(new int(100),myDelete); cout<<*a<<" "<<a.get()<<" "<<a.use_count()<<endl; boost::shared_ptr<int> b(new int(200),myDelete); cout<<*b<<" "<<b.get()<<" "<<b.use_count()<<endl; b=a; cout<<*a<<" "<<a.get()<<" "<<a.use_count()<<endl; cout<<*b<<" "<<b.get()<<" "<<b.use_count()<<endl; //对于没有指定删除器的c来说,当析构时,自动删除对象 boost::shared_ptr<int> c(new int(300)); cout<<*c<<" "<<c.get()<<" "<<c.use_count()<<endl; } int main() { doSharedSomething(); }
相关文章推荐
- 以对象管理资源------auto_ptr shared_ptr
- 以对象管理资源——C++智能指针auto_ptr简介
- C++中的资源管理(一):构造自己的auto_ptr与shared_ptr智能指针
- C++中以对象管理资源<auto_ptr>(13)---《Effective C++》
- effective c++条款13-17 “以对象管理资源”之auto_ptr源码分析
- [置顶] 从零开始学C++之对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
- [置顶] 从零开始学C++之对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector .
- 从零开始学C++之对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
- 对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
- 以对象管理资源——C++智能指针auto_ptr简介
- 多个非同源的shared_ptr管理对象引起double free
- Item 19: 使用srd::shared_ptr来管理共享所有权的资源
- c++11 条款19:使用std::shared_ptr来进行共享所有权的资源管理
- VC++界面编程之--使用STL auto_ptr管理GDI+对象
- 多个非同源的shared_ptr管理对象引起double free
- effective c++条款13-17 “以对象管理资源”之shared_ptr浅析
- shared_ptr:资源管理利器
- Item 19: 使用srd::shared_ptr来管理共享所有权的资源
- shared_ptr:资源管理利器
- Item 19: 使用srd::shared_ptr来管理共享所有权的资源