问题描述
我们知道C ++ 11具有shared_ptr,它将存在循环引用问题。
我试图通过重置类析构函数中的所有成员shared_ptrs来解决此问题。
Example-1 share.cpp
:
#include <iostream>
#include <memory>
#include <string>
class A;
class B;
struct A {
A(const std::string &a_name) : name(a_name),b(nullptr) {
std::cout << name << " A::A b.get:" << b.get()
<< ",b.use_count:" << b.use_count() << std::endl;
}
~A() {
std::cout << name << " A::~A b.get:" << b.get()
<< ",b.use_count:" << b.use_count() << std::endl;
}
std::string name;
std::shared_ptr<B> b;
};
struct B {
B(const std::string &a_name) : name(a_name),a(nullptr) {
std::cout << name << " B::B a.get:" << a.get()
<< ",a.use_count:" << a.use_count() << std::endl;
}
~B() {
std::cout << name << " B::~B a.get:" << a.get()
<< ",a.use_count:" << a.use_count() << std::endl;
}
std::string name;
std::shared_ptr<A> a;
};
int main(void) {
std::shared_ptr<A> a1(new A("a1"));
std::shared_ptr<B> b1(new B("b1"));
a1->b = b1;
b1->a = a1;
{
std::shared_ptr<A> a2(new A("a2"));
std::shared_ptr<B> b2(new B("b2"));
a2->b = b2;
b2->a = a2;
a1->b = b2;
b1->a = a2;
a2->b = b1;
b2->a = a1;
}
{
std::shared_ptr<A> a3(new A("a3"));
std::shared_ptr<B> b3(new B("b3"));
a3->b = b1;
b3->a = a1;
a1->b = b3;
b1->a = a3;
a3->b = b3;
b3->a = a3;
}
return 0;
}
运行:clang++ -std=c++11 share.cpp && ./a.out
:
a1 A::A b.get:0x0,b.use_count:0
b1 B::B a.get:0x0,a.use_count:0
a2 A::A b.get:0x0,b.use_count:0
b2 B::B a.get:0x0,a.use_count:0
a3 A::A b.get:0x0,b.use_count:0
b3 B::B a.get:0x0,a.use_count:0
b2 B::~B a.get:0x7ff393405900,a.use_count:3
a2 A::~A b.get:0x7ff393405950,b.use_count:3
b1 B::~B a.get:0x7ff393405a40,a.use_count:2
a1 A::~A b.get:0x7ff393405a90,b.use_count:2
我们可以看到由于循环引用导致内存泄漏。所以我的想法是:我重置所有类析构函数中的所有成员shared_ptrs。然后我们有Example-2 share.cpp
:
#include <iostream>
#include <memory>
#include <string>
class A;
class B;
struct A {
A(const std::string &a_name) : name(a_name),b.use_count:" << b.use_count() << std::endl;
}
~A() {
std::cout << name << " A::~A before reset b.get:" << b.get()
<< ",b.use_count:" << b.use_count() << std::endl;
b.reset();
std::cout << name << " A::~A after reset b.get:" << b.get()
<< ",a.use_count:" << a.use_count() << std::endl;
}
~B() {
std::cout << name << " B::~B before reset a.get:" << a.get()
<< ",a.use_count:" << a.use_count() << std::endl;
a.reset();
std::cout << name << " B::~B after reset a.get:" << a.get()
<< ",a.use_count:" << a.use_count() << std::endl;
}
std::string name;
std::shared_ptr<A> a;
};
int main(void) {
std::shared_ptr<A> a1(new A("a1"));
std::shared_ptr<B> b1(new B("b1"));
a1->b = b1;
b1->a = a1;
{
std::shared_ptr<A> a2(new A("a2"));
std::shared_ptr<B> b2(new B("b2"));
a2->b = b2;
b2->a = a2;
a1->b = b2;
b1->a = a2;
a2->b = b1;
b2->a = a1;
}
{
std::shared_ptr<A> a3(new A("a3"));
std::shared_ptr<B> b3(new B("b3"));
a3->b = b1;
b3->a = a1;
a1->b = b3;
b1->a = a3;
a3->b = b3;
b3->a = a3;
}
return 0;
}
运行clang++ -std=c++11 share.cpp && ./a.out
:
a1 A::A b.get:0x0,a.use_count:0
b2 B::~B before reset a.get:0x7fdf23405900,a.use_count:3
b2 B::~B after reset a.get:0x0,a.use_count:0
a2 A::~A before reset b.get:0x7fdf23405950,b.use_count:3
a2 A::~A after reset b.get:0x0,b.use_count:0
b1 B::~B before reset a.get:0x7fdf23405a40,a.use_count:2
b1 B::~B after reset a.get:0x0,a.use_count:0
a1 A::~A before reset b.get:0x7fdf23405a90,b.use_count:2
a1 A::~A after reset b.get:0x0,b.use_count:0
我们看到循环参考是固定的!
我可以使用这种设计模式来解决C ++项目中的shared_ptr循环参考问题吗?例如我只是重置了C ++项目中的类析构函数中的所有shared_ptrs。
解决方法
简短的回答:否,因为shared_ptr::reset()
减少了使用计数,就像共享指针析构函数那样。
长答案:阅读作业。
让我们首先考虑A2 / B2案件。分配后,您有2个“链”的对象。 B2-> A1-> B2和A2-> B1-> A2。 A1 / B1共享指针的use_count为2(一个用于链,一个用于局部变量)。 A2 / B2共享指针的使用计数为1(对于链)
因此,您开始第三个程序段
a1->b = b3; // After this assignment,// you've broken the A1->B2 chain,so now B2's use
// count is zero and will be destroyed
b1->a = a3; // Same as above,for the B1->A2 chain,destroying A2
a3->b = b3; // This just assigns A3 to B3
b3->a = a3; // And back B3,forming the A3->B3->A3 chain
现在,当这超出范围时,A3 / B3仍然指向彼此,并被泄漏。对A1 / B1对象的唯一引用是局部变量,它们随后超出范围。
因此,您看到在执行分配时B2和A2对象被破坏,然后B1和A1被破坏。 A3 / B3对象永远不会被破坏。