shared_ptr是一種智能指針(smart pointer)。shared_ptr的作用有如同指針,但會記錄有多少個shared_ptrs共同指向一個對象。
這便是所謂的引用計數(shù)(reference counting)。一旦最后一個這樣的指針被銷毀,也就是一旦某個對象的引用計數(shù)變?yōu)?,這個對象會被自動刪除。這在非環(huán)形數(shù)據(jù)結構中防止資源泄露很有幫助。
auto_ptr由于它的破壞性復制語義,無法滿足標準容器對元素的要求,因而不能放在標準容器中;如果我們希望當容器析構時能自動把它容納的指針元素所指的對象刪除時,通常采用一些間接的方式來實現(xiàn),顯得比較繁瑣。boost庫中提供了一種新型的智能指針shared_ptr,它解決了在多個指針間共享對象所有權的問題,同時也滿足容器對元素的要求,因而可以安全地放入容器中。
總結下幾個使用shared_ptr需要注意的問題:
一. 相互引用鏈
class C;class B : public std::enable_shared_from_this<B>{public: ~B(){ cout << "~B" << endl; } void SetPC(std::shared_ptr<C>& pc){ _pc = pc; } private: std::shared_ptr<C> _pc;};class C : public std::enable_shared_from_this<C>{public: ~C(){ cout << "~C" << endl; } void SetPB(std::shared_ptr<B>& pb){ _pb = pb; } private: std::shared_ptr<B> _pb;};int main(){ std::shared_ptr<C> pc = std::make_shared<C>(); std::shared_ptr<B> pb = std::make_shared<B>(); pc->SetPB(pb); pb->SetPC(pc); return 0;}上面的代碼中,B和C均不能正確析構,正確的做法是,在B和C的釋放函數(shù),如Close中,將其包含的shared_ptr置空。這樣才能解開引用鏈。
二. 自引用
還有個比較有意思的例子:
class C : public std::enable_shared_from_this < C >{public: ~C() { std::cout << "~C" << std::endl; } int32_t Decode(const char* data, size_t) { return 0; } void SetDecoder(std::function<int32_t(const char*, size_t)> decoder) { _decoder = decoder; }private: std::function<int32_t(const char*, size_t)> _decoder;};int main(){ { std::shared_ptr<C> pc = std::make_shared<C>(); auto decoder = std::bind(&C::Decode, pc, std::placeholders::_1, std::placeholders::_2); pc->SetDecoder(decoder); } // C不能正確析構 因為存在自引用 return 0;}上面的C類包含了一個function,該function通過std::bind引用了一個std::shared_ptr,所以_decoder其實包含了一個對shared_ptr的引用。導致C自引用了自身,不能正確析構。需要在C的Close之類的執(zhí)行關閉函數(shù)中,將_decoder=nullptr,以解開這種自引用。
三. 類中傳遞
下面的例子中有個更為隱蔽的問題:
class Session : public std::enable_shared_from_this < Session >{public: ~Session() { std::cout << "~C" << std::endl; } void Start() { // 進行一些異步調用 // 如 _socket.async_connect(..., boost::bind(&Session::ConnectCompleted, this), boost::asio::placeholders::error, ...) } void ConnectCompleted(const boost::system::err_code& err) { if(err) return; // ... 進行處理 // 如 _socket.async_read(..., boost::bind(&Session::ReadCompleted, this), boost::asio::placeholders::error, ...) } void Session::ReadComplete(const boost::system::error_code& err, size_t bytes_transferred) { if (err || bytes_transferred == 0) { DisConnect(); return; } // 處理數(shù)據(jù) 繼續(xù)讀 // ProcessData(); // _socket.async_read(...) }private: std::function<int32_t(const char*, size_t)> _decoder;};int main(){ { std::shared_ptr<Session> pc = std::make_shared<Session>(); pc->Start(); } return 0;}上面Session,在調用Start時,調用了異步函數(shù),并回調自身,如果在回調函數(shù)的 boost::bind 中 傳入的是shared_from_this(),那么并無問題,shared_ptr將被一直傳遞下去,在網(wǎng)絡處理正常時,Session將正常運行,即使main函數(shù)中已經(jīng)沒有它的引用,但是它靠boost::bind”活了下來”,boost::bind會保存?zhèn)鹘o它的shared_ptr,在調用函數(shù)時傳入。當網(wǎng)絡遇到錯誤時,函數(shù)直接返回。此時不再有新的bind為其”續(xù)命”。Session將被析構。
而真正的問題在于,如果在整個bind鏈中,直接傳遞了this指針而不是shared_from_this(),那么實際上當函數(shù)執(zhí)行完成后,Session即會析構,包括其內部的資源(如 _socket)也會被釋放。那么當boost底層去執(zhí)行網(wǎng)絡IO時,自然會遇到錯誤,并且仍然會”正常”回調到對應函數(shù),如ReadCompleted,然后在err中告訴你:”由本地系統(tǒng)終止網(wǎng)絡連接”(或:”An attempt to abort the evaluation failed. The process is now in an indeterminate state.” )。讓人誤以為是網(wǎng)絡問題,很難調試。而事實上此時整個對象都已經(jīng)被釋放掉了。
注:由于C++對象模型實現(xiàn)所致,成員函數(shù)和普通函數(shù)的主要區(qū)別如下:
也就是說,成員函數(shù)并不屬于對象,非靜態(tài)數(shù)據(jù)成員才屬于對象。
因此如下調用在編譯期是合法的:
((A*)nullptr)->Func();
而如果成員函數(shù)A::Func()沒有訪問A的非靜態(tài)成員變量,這段代碼甚至能正確運行,如:
class Test{public: void Say() { std::cout << "Say Test" << std::endl; } void Set(int data) { _data = data; }private: int _data;};int main(){ // 運行成功 ((Test*)nullptr)->Say(); // 運行會崩掉,嘗試訪問空指針所指內存(_data) ((Test*)nullptr)->Set(1); return 0;}正因為這種特性,有時候在成員函數(shù)中糾結半天,也不會注意到這個對象已經(jīng)”不正常了”,被釋放掉了。
四. shared_ptr 使用總結
盡量不要環(huán)引用或自引用,可通過weak_ptr來避免環(huán)引用:owner持有child的shared_ptr child持有owner的weak_ptr
如果存在環(huán)引用或自引用,記得在釋放時解開這個引用鏈
對于通過智能指針管理的類,在類中通過shared_from_this()而不是this來傳遞本身
在類釋放時,盡量手動置空其所有的shared_ptr成員,包括function
新聞熱點
疑難解答