智能指针类模板[图] scoped_ptr[图] boost scoped_ptr的正确构造 boost shared_ptr[图] boost shared_ptr构建.png[图] boost shared_ptr可以多次引用指针 boost week ptr[图] boost week_ptr boost intrusive 侵入式指针 boost make_shared 省略显式的new boost enable_shared_from_this boost 循环引用,对象无法析构,内存泄漏 boost weak_ptr打破循环引用 boost pimpl 接口私有实现技术
智能指针类模板[图]
scoped_ptr[图]
boost scoped_ptr的正确构造
pi@raspberrypi:~/boost $ cat main.cpp // * 操作符重载 // ->操作符重载 #include <iostream> #include <boost/scoped_ptr.hpp> using namespace std; struct A { A() { cout << "A:A()" << endl;} ~A() { cout << "A:~A()" << endl;} void f() { cout << "A:f()" <<endl;} }; //智能指针正确的构造方式 int main() { boost::scoped_ptr<int>sp(new int(128)); cout << ++*sp <<endl; //初始化的方式只有一种,只为独享 //boost::scoped_ptr<int>sp2(sp);//Compile Error boost::scoped_ptr<A> ap(new A); ap->f(); cout << "-------------------" << endl; //演示double free //A a; //栈区变量,离开作用域会自动析构 //boost::scoped_ptr<A>(&a);//离开作用域会自动析构 //Compile Error:note: previous declaration as ‘A a’ return 0; } pi@raspberrypi:~/boost $ g++ -Wall main.cpp &&./a.out 129 A:A() A:f() ------------------- A:~A() pi@raspberrypi:~/boost $
boost shared_ptr[图]
boost shared_ptr构建.png[图]
boost shared_ptr可以多次引用指针
pi@raspberrypi:~/boost $ cat main.cpp #include <iostream> #include <boost/shared_ptr.hpp> using namespace std; struct A { A() { cout << "A:A()" << endl;} ~A() { cout << "A:~A()" << endl;} void f() { cout << "A:f()" <<endl;} }; //智能指针正确的构造方式 int main() { boost::shared_ptr<int>sp(new int(128)); cout << ++*sp <<endl; boost::shared_ptr<int>sp2(sp);//shared_ptr可以多次引用 cout << "sp2的引用次数:" << sp2.use_count() << endl; boost::shared_ptr<A> ap(new A); ap->f(); cout << "-------------------" << endl; return 0; } pi@raspberrypi:~/boost $ g++ -Wall main.cpp &&./a.out 129 sp2的引用次数:2 A:A() A:f() ------------------- A:~A() pi@raspberrypi:~/boost $
boost week ptr[图]
boost week_ptr
pi@raspberrypi:~/boost $ cat main.cpp #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> using namespace std; int main() { boost::shared_ptr<int>sp(new int(128)); cout << ++*sp <<endl; boost::shared_ptr<int>sp2(sp);//shared_ptr可以多次引用 cout << "sp2的引用次数:" << sp2.use_count() << endl; boost::weak_ptr<int> wp(sp2); cout << "wp的引用次数:" <<wp.use_count() << endl; cout << "wp.lock:" <<*wp.lock() << endl;//lock返回一个符合要求的shared_ptr //wp->f();//weak没有->操作符重载,编译报错 cout << "-------------------" << endl; return 0; } pi@raspberrypi:~/boost $ g++ -Wall main.cpp &&./a.out 129 sp2的引用次数:2 wp的引用次数:2 wp.lock:129 ------------------- pi@raspberrypi:~/boost $
boost intrusive 侵入式指针
pi@raspberrypi:~/boost $ cat main.cpp #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/intrusive_ptr.hpp> using namespace std; class ReferenceCounter { public: ReferenceCounter():refCount(0){} virtual ~ReferenceCounter(){} friend void intrusive_ptr_add_ref(ReferenceCounter *p) { ++p->refCount; } friend void intrusive_ptr_release(ReferenceCounter *p) { if(--p->refCount == 0) { delete p; } } int getRefCount() const { return refCount; } private: int refCount; }; class D:public ReferenceCounter { public: D(){cout << "D:D()" << endl;} ~D(){cout << "D:~D()" << endl;} }; int main() { D* dp = new D; boost::intrusive_ptr<D> dp1(dp); {//局域作用域 boost::intrusive_ptr<D> dp2(dp); cout << "dp2 的引用:"<< dp2->getRefCount() << endl; } cout << "dp1 的引用:"<< dp1->getRefCount() << endl; return 0; } pi@raspberrypi:~/boost $ g++ -Wall main.cpp &&./a.out D:D() dp2 的引用:2 dp1 的引用:1 D:~D() pi@raspberrypi:~/boost $
boost make_shared 省略显式的new
#include <iostream> #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> struct D { D(){ std::cout << "D:D()" << std::endl;} D(int k,double d){std::cout << "k="<<k<<" d="<< d << std::endl;} ~D(){std::cout << "D:~D()" << std::endl;} }; struct E { E(std::string s) {std::cout << "s=" <<s<< std::endl;} ~E(){std::cout << "E:~E()" << std::endl;} }; int main() { boost::shared_ptr<D> dp1 = boost::make_shared<D>(); boost::shared_ptr<D> dp2 = boost::make_shared<D>(12,3.14); boost::shared_ptr<E> ep1 = boost::make_shared<E>("Boost"); return 0; } pi@raspberrypi:~/boost $ g++ -Wall main.cpp &&./a.out D:D() k=12 d=3.14 s=Boost E:~E() D:~D() D:~D() pi@raspberrypi:~/boost $
boost enable_shared_from_this
#include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> //在一个类的内部使用shared_ptr class A; void func(boost::shared_ptr<A> p) { std::cout << "sizeof(p)="<<sizeof(p) <<std::endl; } class A:public boost::enable_shared_from_this<A> { public: void f() { std::cout << "A f() \n"; func(shared_from_this()); } void say_hi() { std::cout << "Hello Boost \n"; } }; int main() { boost::shared_ptr <A> a(new A); a->f(); return 0; } pi@raspberrypi:~/boost $ g++ -Wall main.cpp &&./a.out A f() sizeof(p)=8 pi@raspberrypi:~/boost $
boost 循环引用,对象无法析构,内存泄漏
pi@raspberrypi:~/boost $ cat main.cpp #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> struct B; struct A { A() {std::cout <<"A()" <<std::endl;} ~A(){std::cout <<"~A()" <<std::endl;} boost::shared_ptr<B> b; }; struct B { B() {std::cout <<"B()" <<std::endl;} ~B(){std::cout <<"~B()" <<std::endl;} boost::shared_ptr<A> a; }; int main() { boost::shared_ptr<A> ap(new A); boost::shared_ptr<B> bp(new B); ap->b = bp; bp->a = ap; std::cout << "------------\n"; return 0; } pi@raspberrypi:~/boost $ g++ -Wall main.cpp &&./a.out A() B() ------------ pi@raspberrypi:~/boost $
boost weak_ptr打破循环引用
pi@raspberrypi:~/boost $ cat main.cpp #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> struct B; struct A { A() {std::cout <<"A()" <<std::endl;} ~A(){std::cout <<"~A()" <<std::endl;} boost::shared_ptr<B> b; }; struct B { B() {std::cout <<"B()" <<std::endl;} ~B(){std::cout <<"~B()" <<std::endl;} //boost::shared_ptr<A> a; boost::weak_ptr<A> a; }; int main() { boost::shared_ptr<A> ap(new A); boost::shared_ptr<B> bp(new B); ap->b = bp; bp->a = ap; std::cout << "------------\n"; return 0; } pi@raspberrypi:~/boost $ g++ -Wall main.cpp &&./a.out A() B() ------------ ~A() ~B() pi@raspberrypi:~/boost $
boost pimpl 接口私有实现技术
pi@raspberrypi:~/boost $ cat Example.h #ifndef EXAMPLE_H_ #define EXAMPLE_H_ #include <boost/shared_ptr.hpp> class Example { public: Example(); void do_something(); private: class implementation; // hide implementation details boost::shared_ptr<implementation> _imp; }; #endif /* EXAMPLE_H_ */ pi@raspberrypi:~/boost $ cat Example.cpp #include <iostream> #include "Example.h" class Example::implementation { public: ~implementation() { std::cout << "destroying implementation\n"; } }; Example::Example():_imp(new implementation) { } void Example::do_something() { std::cout << "use_count() is " << _imp.use_count() << "\n"; } pi@raspberrypi:~/boost $ cat pimpl_test.cpp #include "Example.h" int main() { Example a; a.do_something(); Example b(a); b.do_something(); Example c; c = a; c.do_something(); return 0; } pi@raspberrypi:~/boost $ g++ -Wall pimpl_test.cpp Example.cpp &&./a.out use_count() is 1 use_count() is 2 destroying implementation use_count() is 3 destroying implementation pi@raspberrypi:~/boost $
本文出自 “魂斗罗” 博客,谢绝转载!
原文地址:http://990487026.blog.51cto.com/10133282/1879969