首頁 > 軟體

C語言 智慧指標 shared_ptr 和 weak_ptr

2022-04-11 22:00:28

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 = 2

reference 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 = 1

B destructor ...
A destructor ...
reference count of A = 0
reference count of B = 0
Hello, world!

到此這篇關於C語言 智慧指標 shared_ptr 和 weak_ptr的文章就介紹到這了,更多相關 shared_ptr 和 weak_ptr內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com