我在用boost::shared_ptr和shared_from_this()时,程序崩溃了。
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
class BaseClass :public boost::enable_shared_from_this<BaseClass>
{
public:
virtual void start()
{
boost::shared_ptr<BaseClass> sharedPtr = shared_from_this();
}
};
typedef boost::shared_ptr<BaseClass> BaseClassPtr;
class DerivedClass :public BaseClass
{
};
typedef boost::shared_ptr<DerivedClass> DerivedClassPtr;
int main()
{
DerivedClassPtr pD = DerivedClassPtr(new DerivedClass);
pD->start();
BaseClassPtr pB = BaseClassPtr(new BaseClass);
pB->start();
DerivedClass d;
d.start();//崩溃
//Unhandled exception at 0x76F7C41F in vc_io_service_reset.exe: Microsoft C++ exception:
//boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> > at memory location 0x0036F0C4.
return 0;
//原因见:http://blog.csdn.net/yockie/article/details/40213331
}
完。