您好,登錄后才能下訂單哦!
這篇文章主要講解了“C語言智能指針shared_ptr和weak_ptr怎么用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“C語言智能指針shared_ptr和weak_ptr怎么用”吧!
weak_ptr引入可以解決shared_ptr交叉引用時無法釋放資源的問題。
示例代碼:
#include <iostream> #include <memory> using namespace std; class B; class A{ public: A(){cout << "A constructor ... "<< endl;} ~A(){cout << "A destructor ..." << endl;} std::shared_ptr<B> pb; }; class B{ public: B(){cout << "B constructor ... "<< endl;} ~B(){cout << "B destructor ..." << endl;} std::shared_ptr<A> pa; }; int main(int argc, char **argv) { std::shared_ptr<int> a = std::make_shared<int>(3); std::shared_ptr<char> b = std::make_shared<char>('a'); std::cout << "shared_ptr object(int) size = " << sizeof(a) << std::endl; std::cout << "shared_ptr object(char) size = " << sizeof(b) << std::endl; std::weak_ptr<A> shadow_a; std::weak_ptr<B> shadow_b; { std::shared_ptr<A> ptr_a = std::make_shared<A>(); std::shared_ptr<B> ptr_b = std::make_shared<B>(); shadow_a = ptr_a; shadow_b = ptr_b; ptr_a->pb = ptr_b; ptr_b->pa = ptr_a; cout << "reference count of A = " << shadow_a.use_count() << endl; cout << "reference count of B = " << shadow_b.use_count() << endl; cout << endl; } cout << "reference count of A = " << shadow_a.use_count() << endl; cout << "reference count of B = " << shadow_b.use_count() << endl; std::cout << "Hello, world!" << std::endl; return 0; }
運行代碼得到以下輸出:
shared_ptr object(int) size = 16
shared_ptr object(char) size = 16
A constructor ...
B constructor ...
reference count of A = 2
reference count of B = 2reference count of A = 1
reference count of B = 1
Hello, world!
從結果可以看出,由于交叉引用導致申請的內存A,B無法正常釋放。
為什么會這樣呢?這個應該從析構原理進行考慮,shared_ptr引用計數需要為0才會進行析構!但是ptr_a離開作用域會導致A引用計數減少1,但是A的引用計數此時為1,那么 pb不會釋放;同理,ptr_b離開作用域會導致B引用計數減少1,但是B的引用計數為此時為1,那么pa不會釋放。如此導致了資源無法釋放掉。
由于weak_ptr并不會改變shared_ptr的引用計數,所以修改類A,和類B中的shared_ptr對象為weak_ptr對象即可釋放資源。
修改后的代碼如下:
#include <iostream> #include <memory> using namespace std; class B; class A{ public: A(){cout << "A constructor ... "<< endl;} ~A(){cout << "A destructor ..." << endl;} //std::shared_ptr<B> pb; std::weak_ptr<B> pb; }; class B{ public: B(){cout << "B constructor ... "<< endl;} ~B(){cout << "B destructor ..." << endl;} //std::shared_ptr<A> pa; std::weak_ptr<A> pa; }; int main(int argc, char **argv) { std::shared_ptr<int> a = std::make_shared<int>(3); std::shared_ptr<char> b = std::make_shared<char>('a'); std::cout << "shared_ptr object(int) size = " << sizeof(a) << std::endl; std::cout << "shared_ptr object(char) size = " << sizeof(b) << std::endl; std::weak_ptr<A> shadow_a; std::weak_ptr<B> shadow_b; { std::shared_ptr<A> ptr_a = std::make_shared<A>(); std::shared_ptr<B> ptr_b = std::make_shared<B>(); shadow_a = ptr_a; shadow_b = ptr_b; ptr_a->pb = ptr_b; ptr_b->pa = ptr_a; cout << "reference count of A = " << shadow_a.use_count() << endl; cout << "reference count of B = " << shadow_b.use_count() << endl; cout << endl; } cout << "reference count of A = " << shadow_a.use_count() << endl; cout << "reference count of B = " << shadow_b.use_count() << endl; std::cout << "Hello, world!" << std::endl; return 0; }
運行結果如下,可以正常釋放資源。
shared_ptr object(int) size = 16
shared_ptr object(char) size = 16
A constructor ...
B constructor ...
reference count of A = 1
reference count of B = 1B destructor ...
A destructor ...
reference count of A = 0
reference count of B = 0
Hello, world!
感謝各位的閱讀,以上就是“C語言智能指針shared_ptr和weak_ptr怎么用”的內容了,經過本文的學習后,相信大家對C語言智能指針shared_ptr和weak_ptr怎么用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。