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;
}