智能指針share_ptr,與unique_ptr不同,多個shar_ptr對象可以共同管理一個指針,它們通過一個共同的引用計數器來管理指針。當一個智能指針對象銷毀時,計數器減一。當計數器為0時,會將所指向的內存對象釋放。
#include<memory>
#include<iostream>
#include<vector>
#include<algorithm>
#include<chrono>
using namespace std;class Rectangle {public:Rectangle(double w, double h) :width(w), height(h) {}~Rectangle() {}double area(){return width * height;}
private:double width;double height;
};
int main()
{using std::shared_ptr;shared_ptr<Rectangle>p1(new Rectangle(1, 5));shared_ptr<Rectangle>p2 = p1;shared_ptr<Rectangle>p3(p2);cout << p1.use_count() << endl;
}
打印結果?
?另外share_ptr提供幾個輔助函數,用于對封裝指針類型得靜態轉換,動態轉換以及常量轉換,它們分別是dynamic_pointer_cast,static_pointer_cast和const_point_cast.
#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class Base {
public:virtual ~Base() {}};
class Derive :public Base {};int main()
{shared_ptr<Base>sp1(new Derive());shared_ptr<Derive>sp2 = dynamic_pointer_cast<Derive>(sp1);shared_ptr<Base>sp3 = static_pointer_cast<Base>(sp2);cout << "use count:" << sp1.use_count() << endl;
雖然他們封裝得對象類型不同,但他們都是指向同一個對象,所以引用計數為3.
在使用share_ptr時容易出現一個問題,就是當出現循環引用時,無法釋放所指向的資源。造成內存泄漏。
#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class Person
{
public:Person(string name) :m_name(name) { cout << m_name << "constructed" << endl;}~Person(){cout << m_name << "desconstructed" << endl;}void setParter(const shared_ptr<Person>& other){m_partner = other;}
private:string m_name;shared_ptr<Person>m_partner;
};int main()
{vector<shared_ptr<Person>>people;people.push_back(shared_ptr<Person>(new Person("張三")));people.push_back(shared_ptr<Person>(new Person("李四")));people.push_back(shared_ptr<Person>(new Person("王五")));people[0]->setParter(people[1]);people[1]->setParter(people[2]);people[2]->setParter(people[0]);return 0;
}
打印結果
這里people存放了三個person對象,由于person 的成員變量m_partner也是指向Person對象的共享智能指針,接下來這三條語句,peoeple中的第一個元素的m_partner指向了第二個元素中的Person對象,第二個元素的m_partner指向了第三個元素中Person的對象,第三個元素的m_partner指向了第一個元素中的Person對象。這樣每個對象的引用計數都是2.當people向量離開作用域銷毀后,會將每個對象的引用計數減1,但每個對象的成員m_partner仍存在,所以無法刪除Person對象,最終導致內存的泄露。為了防止這種情況出現,就避免出現share_ptr循環引用情況。或者結合使用另外一種智能指針weak_ptr;例如將Person成員變量中的share_ptr改為weak_ptr,當peolple離開作用域銷毀后,他所指向的對象也都自動銷毀。
weak_ptr不能單獨使用,需要結合share_ptr一起使用。我weak_ptr的對象可以將share_ptr作為構造函數的參數初始化,或者定義一個空的weak_ptr,然后將share_ptr對象賦值給它。如下代碼
#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class A {};
int main()
{shared_ptr<A> sp1 = make_shared<A>();weak_ptr<A>wp1(sp1);weak_ptr<A>wp2;wp2 =sp1;cout << "use count: " << wp2.use_count() << endl;}
打印結果
weak_ptr的主要特征是,只對share_ptr所管理的的對象進行觀測,不會改變對象的引用計數。如下代碼
#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class Rectangle {public:Rectangle(double w, double h) :width(w), height(h) {}~Rectangle() {}double area(){return width * height;}
private:double width;double height;
};
int main()
{weak_ptr<Rectangle>w_sp1;{shared_ptr<Rectangle>sp1(new Rectangle(1, 2));shared_ptr<Rectangle>sp2 = sp1;w_sp1 = sp2;cout << "作用域內部usecount=" << w_sp1.use_count() << endl;}cout << "作用域外部usercount=" << w_sp1.use_count() << endl;cout << "expired=" << w_sp1.expired() << endl;//為1時所觀測的對象不可用
}
打印結果
我們可以通過weak_ptr使用lock函數來獲得一個shared_ptr以獲得封裝對象的控制權。在下面這個例子里我們輸出share_ptr里封裝對象指針,由于share_ptr重載了插入運算符,所以可以直接打印出封裝的指針的值。
#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class Rectangle {public:Rectangle(double w, double h) :width(w), height(h) {}~Rectangle() {}double area(){return width * height;}
private:double width;double height;
};
int main()
{weak_ptr<Rectangle>w_sp1;{shared_ptr<Rectangle>sp1(new Rectangle(1, 2));shared_ptr<Rectangle>sp2 = sp1;w_sp1 = sp2;shared_ptr<Rectangle>sp3 = w_sp1.lock();cout << "作用域內部sp3=" << sp3 << endl;}shared_ptr<Rectangle>sp3 = w_sp1.lock();cout << "作用域外部sp3=" << sp3 << endl;cout << "expired=" << w_sp1.expired() << endl;//為1時所觀測的對象不可用
}
打印結果
可以看到在作用域里面share_ptr是個有效指針,而在作用域外是個空指針。所以當我們使用weak_ptr觀察對象是否為空指針時,并不需要使用use_count或expired來判斷 ,而是可以直接調用lock函數獲得一個share_ptr對象,如果share_ptr包含的指針非空那么就可用。由于lock對象是個原子操作,是一個不可分割的操作,當原子操作執行進程中時,其它線程不能讀取,修改或者中斷操作的數據,因此,使用lock函數保證了多線程時獲得的share_ptr中的對象是安全有效的,此外,在多線程環境下,當使用expired函數時,只有當expired返回true時才是有意義的。
下面時weak_ptr和share_ptr直接的關系。