shared_ptr编译报错-求助大神

shared_ptr 的相互拥抱,导致无法析构

#include <memory>
#include <iostream>
class TestB;

class TestA
{
public:
    TestA() {
        std::cout << "TestA()" << std::endl;
    }
    void ReferTestB(std::shared_ptr<TestB> test_ptr) {
        m_TestB_Ptr = test_ptr;
    }
    ~TestA() {
        std::cout << "~TestA()" << std::endl;
    }
private:
    std::shared_ptr<TestB> m_TestB_Ptr; //TestB的智能指针
};

class TestB
{
public:
    TestB() {
        std::cout << "TestB()" << std::endl;
    }
    void ReferTestA(std::shared_ptr<TestA> test_ptr) {
        m_TestA_Ptr = test_ptr;
    }
    ~TestB() {
        std::cout << "~TestB()" << std::endl;
    }
    std::shared_ptr<TestA> m_TestA_Ptr; //TestA的智能指针
};
int main()
{
    std::shared_ptr<TestA> ptr_a = std::make_shared<TestA>();
    std::shared_ptr<TestB> ptr_b = std::make_shared<TestB>();
    ptr_a->ReferTestB(ptr_b);
    ptr_b->ReferTestA(ptr_a);
    return 0;
}

//================================================================

通过 weak_ptr 解决这个问题

#include <iostream>
#include <memory>

using namespace std;

class TestB;

class TestA
{
public:
    TestA() {
        cout << __FUNCTION__ << ", construct" << endl;
    }
    void ReferTestB(shared_ptr<TestB> test_ptr) {
        m_TestB_Ptr = test_ptr;
    }
    void TestWork() {
        cout << __FUNCTION__ << endl;
    }
    ~TestA() {
        cout << __FUNCTION__ << ", before destroy" << endl;
        cout << __FUNCTION__ << ", m_TestB_Ptr's ref_count is: " << m_TestB_Ptr.use_count() << endl;

        cout << __FUNCTION__ << ", after destroy" << endl;
    }
private:
    weak_ptr<TestB> m_TestB_Ptr;
};

class TestB
{
public:
    TestB() {
        cout << __FUNCTION__ << ", construct" << endl;
    }
    void ReferTestA(shared_ptr<TestA> test_ptr) {
        m_TestA_Ptr = test_ptr;
    }
    void TestWork() {
        cout << __FUNCTION__ << endl;
    }
    ~TestB() {
        cout << __FUNCTION__ << ", before destroy" << endl;
        cout << __FUNCTION__ << ", m_TestA_Ptr's ref_count is: " << m_TestA_Ptr.use_count() << endl;

        //把weak_ptr类型转换成shared_ptr类型
        shared_ptr<TestA> tmp = m_TestA_Ptr.lock();
        if (tmp != NULL) {
            tmp->TestWork();
            cout << __FUNCTION__ << ", tmp's ref_count is: " << tmp.use_count() << endl;
            cout << __FUNCTION__ << ", m_TestA_Ptr's ref_count is: " << m_TestA_Ptr.use_count() << endl;
        }
        else {
            cout << __FUNCTION__ << ", m_TestA_Ptr has destroyed" << endl;
        }
        cout << __FUNCTION__ << ", after destroy" << endl;
    }

    weak_ptr<TestA> m_TestA_Ptr;
};

int main()
{
    shared_ptr<TestA> ptr_a = make_shared<TestA>();
    cout << __FUNCTION__ << ", ptr_a's ref_count: " << ptr_a.use_count() << endl;
    shared_ptr<TestB> ptr_b = make_shared<TestB>();
    cout << __FUNCTION__ << ", ptr_b's ref_count: " << ptr_b.use_count() << endl;

    ptr_a->ReferTestB(ptr_b); // 将 ptr_b 赋给 m_TestB_Ptr (weak_ptr)
    cout << __FUNCTION__ << ", ptr_b's ref_count: " << ptr_b.use_count() << endl;

    ptr_b->ReferTestA(ptr_a); // 将 ptr_a 赋给 m_TestA_Ptr (weak_ptr)
    cout << __FUNCTION__ << ", ptr_a's ref_count: " << ptr_a.use_count() << endl;

    return 0;
}

//================================================================

但如下一直编译不过,不知道为啥

#include <iostream>
#include <memory>

using namespace std;

class TestB;

class TestA
{
public:
    TestA() {
        cout << __FUNCTION__ << ", construct" << endl;
    }
    void ReferTestB(shared_ptr<TestB> test_ptr) {
        m_TestB_Ptr = test_ptr;
    }
    void TestWork() {
        cout << __FUNCTION__ << endl;
    }
    ~TestA() {
        cout << __FUNCTION__ << ", before destroy" << endl;
        cout << __FUNCTION__ << ", m_TestB_Ptr's ref_count is: " << m_TestB_Ptr.use_count() << endl;

        //把weak_ptr类型转换成shared_ptr类型
		// 如果这里升级 m_TestB_Ptr
		// 编译不过,不知道为啥,哪路大神帮忙看下
		// 单纯从C++语言的角度出发,而不是代码业务逻辑角度出发
        shared_ptr<TestB> tmp = m_TestB_Ptr.lock();
        if (tmp != NULL) {
            tmp->TestWork();
            cout << __FUNCTION__ << ", tmp's ref_count is: " << tmp.use_count() << endl;
            cout << __FUNCTION__ << ", m_TestB_Ptr's ref_count is: " << m_TestB_Ptr.use_count() << endl;
        }
        else {
            cout << __FUNCTION__ << ", m_TestB_Ptr has destroyed" << endl;
        }
        cout << __FUNCTION__ << ", after destroy" << endl;
    }
private:
    weak_ptr<TestB> m_TestB_Ptr;
};

class TestB
{
public:
    TestB() {
        cout << __FUNCTION__ << ", construct" << endl;
    }
    void ReferTestA(shared_ptr<TestA> test_ptr) {
        m_TestA_Ptr = test_ptr;
    }
    void TestWork() {
        cout << __FUNCTION__ << endl;
    }
    ~TestB() {
        cout << __FUNCTION__ << ", before destroy" << endl;
        cout << __FUNCTION__ << ", m_TestA_Ptr's ref_count is: " << m_TestA_Ptr.use_count() << endl;

        //把weak_ptr类型转换成shared_ptr类型
        shared_ptr<TestA> tmp = m_TestA_Ptr.lock();
        if (tmp != NULL) {
            tmp->TestWork();
            cout << __FUNCTION__ << ", tmp's ref_count is: " << tmp.use_count() << endl;
            cout << __FUNCTION__ << ", m_TestA_Ptr's ref_count is: " << m_TestA_Ptr.use_count() << endl;
        }
        else {
            cout << __FUNCTION__ << ", m_TestA_Ptr has destroyed" << endl;
        }
        cout << __FUNCTION__ << ", after destroy" << endl;
    }

    weak_ptr<TestA> m_TestA_Ptr;
};

int main()
{
    shared_ptr<TestA> ptr_a = make_shared<TestA>();
    cout << __FUNCTION__ << ", ptr_a's ref_count: " << ptr_a.use_count() << endl;
    shared_ptr<TestB> ptr_b = make_shared<TestB>();
    cout << __FUNCTION__ << ", ptr_b's ref_count: " << ptr_b.use_count() << endl;

    ptr_a->ReferTestB(ptr_b); // 将 ptr_b 赋给 m_TestB_Ptr (weak_ptr)
    cout << __FUNCTION__ << ", ptr_b's ref_count: " << ptr_b.use_count() << endl;

    ptr_b->ReferTestA(ptr_a); // 将 ptr_a 赋给 m_TestA_Ptr (weak_ptr)
    cout << __FUNCTION__ << ", ptr_a's ref_count: " << ptr_a.use_count() << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值