1.先来看这样一个例子(用一个裸指针初始化两个shared_ptr)
不给自己留机会(乱用raw指针的机会)犯错。
运行结果:
可见只有继承了enable_shared_from_this<T>的类型(如这里的Good类)才可以在类里面(类里面只有裸指针this,没有用this初始化的那个shared_ptr呦!)从容的得到”另外的“(非首个的、类似于gp2 )shared_ptr。
#include <memory>
#include <iostream>
using namespace std;
struct Good: std::enable_shared_from_this<Good>
{
std::shared_ptr<Good> getptr() {
return shared_from_this();
}
~Good (){
cout << "Good:~Good() called" << endl;
}
};
int main()
{
// Good: the two shared_ptr's share the same object
Good* raw = new Good;
shared_ptr<Good> sptr(raw);
shared_ptr<Good> sptr2_bad(raw);
cout << "sptr.use_cout()->" << sptr.use_count() << endl;
cout << "sptr2_bad.use_cout()->" << sptr2_bad.use_count() << endl;}
运行结果:
sptr.use_cout()->1
sptr2_bad.use_cout()->1
Good:~Good() called
Good:~Good() called//注释:释放了两次!!
而且程序运行异常,报错。很显然,用一个raw指针初始化两个shared_ptr会引发错误,每个shared_ptr都认为自已是raw指针的独有者,都会在自己销毁的时候去自认为”尽职“的去释放那个裸指针,导致裸指针被重复释放而报错。
这也提示我们,不要这样初始化shared_ptr,以防我们再次乱用raw指针
Good* raw = new Good;
shared_ptr<Good> sptr(raw);
而要
std::shared_ptr<Good> gp1(new Good);
不给自己留机会(乱用raw指针的机会)犯错。
2. 理解了这个例子再看shared_from_this就容易了。
#include <memory>
#include <iostream>
struct Good: std::enable_shared_from_this<Good>
{
std::shared_ptr<Good> getptr() {
return shared_from_this();
}
};
struct Bad
{
std::shared_ptr<Bad> getptr() {
return std::shared_ptr<Bad>(this);
}
~Bad() { std::cout << "Bad::~Bad() called\n"; }
};
int main()
{
// Good: the two shared_ptr's share the same object
std::shared_ptr<Good> gp1(new Good);
std::shared_ptr<Good> gp2 = gp1->getptr();
std::cout << "gp2.use_count() = " << gp2.use_count() << '\n';
// Bad, each shared_ptr thinks it's the only owner of the object
std::shared_ptr<Bad> bp1(new Bad);
std::shared_ptr<Bad> bp2 = bp1->getptr();
std::cout << "bp2.use_count() = " << bp2.use_count() << '\n';
} // UB: double-delete of Bad
运行结果:
gp2.use_count() = 2
bp2.use_count() = 1
Bad::~Bad() called
Bad::~Bad() called
*** glibc detected *** ./test: double free or corruption
可见只有继承了enable_shared_from_this<T>的类型(如这里的Good类)才可以在类里面(类里面只有裸指针this,没有用this初始化的那个shared_ptr呦!)从容的得到”另外的“(非首个的、类似于gp2 )shared_ptr。