一. scoped_ptr
boost::scoped_ptr和std::auto_ptr非常類似,是一個簡單的智能指針,它能夠保證在離開作用域后對象被自動釋放。下列代碼演示了該指針的基本應用:
class implementation
{
public:
~implementation() { std::cout <<"destroying implementation/n"; }
void do_something() { std::cout << "did something/n"; }
};
void test()
{
boost::scoped_ptr<implementation> impl(new implementation());
impl->do_something();
}
void main()
{
std::cout<<"Test Begin ... /n";
test();
std::cout<<"Test End./n";
}
boost::scoped_ptr特點:
boost::scoped_ptr的實現和std::auto_ptr非常類似,都是利用了一個棧上的對象去管理一個堆上的對象,從而使得堆上的對象隨著棧上的對象銷毀時自動刪除。不同的是,boost::scoped_ptr有著更嚴格的使用限制――不能拷貝。這就意味著:boost::scoped_ptr指針是不能轉換其所有權的。
1.不能轉換所有權
boost::scoped_ptr所管理的對象生命周期僅僅局限于一個區間(該指針所在的"{}"之間),無法傳到區間之外,這就意味著boost::scoped_ptr對象是不能作為函數的返回值的(std::auto_ptr可以)。
2.不能共享所有權
這點和std::auto_ptr類似。這個特點一方面使得該指針簡單易用。另一方面也造成了功能的薄弱――不能用于stl的容器中。
3.不能用于管理數組對象
由于boost::scoped_ptr是通過delete來刪除所管理對象的,而數組對象必須通過deletep[]來刪除,因此boost::scoped_ptr是不能管理數組對象的,如果要管理數組對象需要使用boost::scoped_array類。
boost::scoped_ptr的常用操作:
可以簡化為如下形式:
template<typename T> class scoped_ptr : noncopyable {
public:
explicit scoped_ptr(T* p = 0);
~scoped_ptr();
void reset(T* p = 0);
T& operator*() const;
T* operator->() const;
T* get() const;
void swap(scoped_ptr& b);
};
template<typename T>
void swap(scoped_ptr<T> & a, scoped_ptr<T> & b);
}
成員函數 | 功能 |
operator*() | 以引用的形式訪問所管理的對象的成員 |
operator->() | 以指針的形式訪問所管理的對象的成員 |
get() | 釋放所管理的對象,管理另外一個對象 |
swap(scoped_ptr& b) | 交換兩個boost::scoped_ptr管理的對象 |
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <boost/config.hpp>
#include <boost/detail/lightweight_test.hpp>
void test()
{
// test scoped_ptr with a built-in type
long * lp = new long;
boost::scoped_ptr<long> sp ( lp );
BOOST_TEST( sp.get() == lp );
BOOST_TEST( lp == sp.get() );
BOOST_TEST( &*sp == lp );
*sp = 1234568901L;
BOOST_TEST( *sp == 1234568901L );
BOOST_TEST( *lp == 1234568901L );
long * lp2 = new long;
boost::scoped_ptr<long> sp2 ( lp2 );
sp.swap(sp2);
BOOST_TEST( sp.get() == lp2 );
BOOST_TEST( sp2.get() == lp );
sp.reset(NULL);
BOOST_TEST( sp.get() == NULL );
}
void main()
{
test();
}
二. shared_ptr
boost::scoped_ptr雖然簡單易用,但它不能共享所有權的特性卻大大限制了其使用范圍,而boost::shared_ptr可以解決這一局限。顧名思義,boost::shared_ptr是可以共享所有權的智能指針,首先讓我們通過一個例子看看它的基本用法:
class implementation
{
public:
~implementation() { std::cout <<"destroying implementation/n"; }
void do_something() { std::cout << "did something/n"; }
};
void test()
{
boost::shared_ptr<implementation> sp1(new implementation());
std::cout<<"The Sample now has "<<sp1.use_count()<<" references/n";
boost::shared_ptr<implementation> sp2 = sp1;
std::cout<<"The Sample now has "<<sp2.use_count()<<" references/n";
sp1.reset();
std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<<" references/n";
sp2.reset();
std::cout<<"After Reset sp2./n";
}
void main()
{
test();
}
boost::shared_ptr的內存管理機制:
boost::shared_ptr的管理機制其實并不復雜,就是對所管理的對象進行了引用計數,當新增一個boost::shared_ptr對該對象進行管理時,就將該對象的引用計數加一;減少一個boost::shared_ptr對該對象進行管理時,就將該對象的引用計數減一,如果該對象的引用計數為0的時候,說明沒有任何指針對其管理,才調用delete釋放其所占的內存。
上面的那個例子可以的圖示如下:
1.sp1對implementation對象進行管理,其引用計數為1
2.增加sp2對implementation對象進行管理,其引用計數增加為2
3.sp1釋放對implementation對象進行管理,其引用計數變為1
4.sp2釋放對implementation對象進行管理,其引用計數變為0,該對象被自動刪除
boost::shared_ptr的特點:
和前面介紹的boost::scoped_ptr相比,boost::shared_ptr可以共享對象的所有權,因此其使用范圍基本上沒有什么限制(還是有一些需要遵循的使用規則,下文中介紹),自然也可以使用在stl的容器中。另外它還是線程安全的,這點在多線程程序中也非常重要。
boost::shared_ptr的使用規則:
boost::shared_ptr并不是絕對安全,下面幾條規則能使我們更加安全的使用boost::shared_ptr:
1.避免對shared_ptr所管理的對象的直接內存管理操作,以免造成該對象的重釋放
2.shared_ptr并不能對循環引用的對象內存自動管理(這點是其它各種引用計數管理內存方式的通?。?BR>3.不要構造一個臨時的shared_ptr作為函數的參數。
如下列代碼則可能導致內存泄漏:
循環引用:
引用計數是一種便利的內存管理機制,但它有一個很大的缺點,那就是不能管理循環引用的對象。一個簡單的例子如下:
class parent;
class children;
typedef boost::shared_ptr<parent> parent_ptr;
typedef boost::shared_ptr<children> children_ptr;
class parent
{
public:
~parent() { std::cout <<"destroying parent/n"; }
public:
children_ptr children;
};
class children
{
public:
~children() { std::cout <<"destroying children/n"; }
public:
parent_ptr parent;
};
void test()
{
parent_ptr father(new parent());
children_ptr son(new children);
father->children = son;
son->parent = father;
}
void main()
{
std::cout<<"begin test.../n";
test();
std::cout<<"end test./n";
}
一般來講,解除這種循環引用有下面有三種可行的方法:
1.當只剩下最后一個引用的時候需要手動打破循環引用釋放對象。
2.當parent的生存期超過children的生存期的時候,children改為使用一個普通指針指向parent。
3.使用弱引用的智能指針打破這種循環引用。
雖然這三種方法都可行,但方法1和方法2都需要程序員手動控制,麻煩且容易出錯。這里主要介紹一下第三種方法和boost中的弱引用的智能指針boost::weak_ptr。
強引用和弱引用
一個強引用當被引用的對象活著的話,這個引用也存在(就是說,當至少有一個強引用,那么這個對象就不能被釋放)。boost::share_ptr就是強引用。
相對而言,弱引用當引用的對象活著的時候不一定存在。僅僅是當它存在的時候的一個引用。弱引用并不修改該對象的引用計數,這意味這弱引用它并不對對象的內存進行管理,在功能上類似于普通指針,然而一個比較大的區別是,弱引用能檢測到所管理的對象是否已經被釋放,從而避免訪問非法內存。
boost::weak_ptr<T>是boost提供的一個弱引用的智能指針,它的聲明可以簡化如下:
namespace boost {
template<typename T> class weak_ptr {
public:
template <typename Y>
weak_ptr(const shared_ptr<Y>& r);
weak_ptr(const weak_ptr& r);
~weak_ptr();
T* get() const;
bool expired() const;
shared_ptr<T> lock() const;
};
}
boost::weak_ptr除了對所管理對象的基本訪問功能(通過get()函數)外,還有兩個常用的功能函數:expired()用于檢測所管理的對象是否已經釋放;lock()用于獲取所管理的對象的強引用指針。
通過boost::weak_ptr來打破循環引用
由于弱引用不更改引用計數,類似普通指針,只要把循環引用的一方使用弱引用,即可解除循環引用。對于上面的那個例子來說,只要把children的定義改為如下方式,即可解除循環引用:
public:
boost::weak_ptr<parent> parent;
};
|
新聞熱點
疑難解答
圖片精選