欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C++中shared_ptr的使用教程

 更新時(shí)間:2016年05月31日 18:37:12   作者:wudaijun  
shared_ptr能夠記錄對象被引用的次數(shù),主要被用來管理動態(tài)創(chuàng)建的對象的銷毀,這里我們就來詳解C++中shared_ptr的使用教程,需要的朋友可以參考下

shared_ptr是一種智能指針(smart pointer)。shared_ptr的作用有如同指針,但會記錄有多少個(gè)shared_ptrs共同指向一個(gè)對象。
這便是所謂的引用計(jì)數(shù)(reference counting)。一旦最后一個(gè)這樣的指針被銷毀,也就是一旦某個(gè)對象的引用計(jì)數(shù)變?yōu)?,這個(gè)對象會被自動刪除。這在非環(huán)形數(shù)據(jù)結(jié)構(gòu)中防止資源泄露很有幫助。
auto_ptr由于它的破壞性復(fù)制語義,無法滿足標(biāo)準(zhǔn)容器對元素的要求,因而不能放在標(biāo)準(zhǔn)容器中;如果我們希望當(dāng)容器析構(gòu)時(shí)能自動把它容納的指針元素所指的對象刪除時(shí),通常采用一些間接的方式來實(shí)現(xiàn),顯得比較繁瑣。boost庫中提供了一種新型的智能指針shared_ptr,它解決了在多個(gè)指針間共享對象所有權(quán)的問題,同時(shí)也滿足容器對元素的要求,因而可以安全地放入容器中。

總結(jié)下幾個(gè)使用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均不能正確析構(gòu),正確的做法是,在B和C的釋放函數(shù),如Close中,將其包含的shared_ptr置空。這樣才能解開引用鏈。

二. 自引用
還有個(gè)比較有意思的例子:

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不能正確析構(gòu) 因?yàn)榇嬖谧砸?
 return 0;
}

上面的C類包含了一個(gè)function,該function通過std::bind引用了一個(gè)std::shared_ptr,所以_decoder其實(shí)包含了一個(gè)對shared_ptr的引用。導(dǎo)致C自引用了自身,不能正確析構(gòu)。需要在C的Close之類的執(zhí)行關(guān)閉函數(shù)中,將_decoder=nullptr,以解開這種自引用。

三. 類中傳遞
下面的例子中有個(gè)更為隱蔽的問題:

class Session : public std::enable_shared_from_this < Session >
{
public:

 ~Session()
 {
  std::cout << "~C" << std::endl;
 }

 void Start()
 {
  // 進(jìn)行一些異步調(diào)用
  // 如 _socket.async_connect(..., boost::bind(&Session::ConnectCompleted, this), boost::asio::placeholders::error, ...)
 }

 void ConnectCompleted(const boost::system::err_code& err)
 {
 if(err)
 return; 

  // ... 進(jìn)行處理
  // 如 _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,在調(diào)用Start時(shí),調(diào)用了異步函數(shù),并回調(diào)自身,如果在回調(diào)函數(shù)的 boost::bind 中 傳入的是shared_from_this(),那么并無問題,shared_ptr將被一直傳遞下去,在網(wǎng)絡(luò)處理正常時(shí),Session將正常運(yùn)行,即使main函數(shù)中已經(jīng)沒有它的引用,但是它靠boost::bind”活了下來”,boost::bind會保存?zhèn)鹘o它的shared_ptr,在調(diào)用函數(shù)時(shí)傳入。當(dāng)網(wǎng)絡(luò)遇到錯(cuò)誤時(shí),函數(shù)直接返回。此時(shí)不再有新的bind為其”續(xù)命”。Session將被析構(gòu)。
而真正的問題在于,如果在整個(gè)bind鏈中,直接傳遞了this指針而不是shared_from_this(),那么實(shí)際上當(dāng)函數(shù)執(zhí)行完成后,Session即會析構(gòu),包括其內(nèi)部的資源(如 _socket)也會被釋放。那么當(dāng)boost底層去執(zhí)行網(wǎng)絡(luò)IO時(shí),自然會遇到錯(cuò)誤,并且仍然會”正?!被卣{(diào)到對應(yīng)函數(shù),如ReadCompleted,然后在err中告訴你:”由本地系統(tǒng)終止網(wǎng)絡(luò)連接”(或:”An attempt to abort the evaluation failed. The process is now in an indeterminate state.” )。讓人誤以為是網(wǎng)絡(luò)問題,很難調(diào)試。而事實(shí)上此時(shí)整個(gè)對象都已經(jīng)被釋放掉了。
注:由于C++對象模型實(shí)現(xiàn)所致,成員函數(shù)和普通函數(shù)的主要區(qū)別如下:

  • 成員函數(shù)帶隱式this參數(shù)
  • 成員函數(shù)具有訪問作用域,并且函數(shù)內(nèi)會對非靜態(tài)成員變量訪問做一些轉(zhuǎn)換,如 _member_data 轉(zhuǎn)換成 this->_member_data;

也就是說,成員函數(shù)并不屬于對象,非靜態(tài)數(shù)據(jù)成員才屬于對象。

因此如下調(diào)用在編譯期是合法的:

((A*)nullptr)->Func();

而如果成員函數(shù)A::Func()沒有訪問A的非靜態(tài)成員變量,這段代碼甚至能正確運(yùn)行,如:

class Test
{


public:
 void Say()
 {
  std::cout << "Say Test" << std::endl;
 }

 void Set(int data)
 {
  _data = data;
 }

private:
 int _data;
};
int main()
{
 // 運(yùn)行成功
 ((Test*)nullptr)->Say();
 // 運(yùn)行會崩掉,嘗試訪問空指針?biāo)竷?nèi)存(_data)
 ((Test*)nullptr)->Set(1);
 return 0;
}


正因?yàn)檫@種特性,有時(shí)候在成員函數(shù)中糾結(jié)半天,也不會注意到這個(gè)對象已經(jīng)”不正常了”,被釋放掉了。

四. shared_ptr 使用總結(jié)
盡量不要環(huán)引用或自引用,可通過weak_ptr來避免環(huán)引用:owner持有child的shared_ptr child持有owner的weak_ptr
如果存在環(huán)引用或自引用,記得在釋放時(shí)解開這個(gè)引用鏈
對于通過智能指針管理的類,在類中通過shared_from_this()而不是this來傳遞本身
在類釋放時(shí),盡量手動置空其所有的shared_ptr成員,包括function

相關(guān)文章

最新評論