boost::share_ptr的缺陷
share_ptr存在巨大缺陷
CObject* pobject = new CObject();
share_ptr<CObject> p1 = pobject; or share_ptr<CObject> p1(pobject);
share_ptr<CObject> p2 = pobject; or share_ptr<CObject> p2(pobject);
这样子的话计数就会错误,哈哈哈
谁叫他把计数放到指针类里
且看share_ptr的构造函数:
template<class Y>
explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
{
boost::detail::sp_pointer_construct( this, p, pn );
}
template<class Y>
shared_ptr & operator=( std::auto_ptr<Y> & r )
{
this_type( r ).swap( *this );
return *this;
}
看到没,pn(引用计数)每次都是重新new的,计数就错了(pn是个很复杂的东西,归根结底它用来计数)
具体没有测试过。。。

浙公网安备 33010602011771号