We know that C++11 has shared_ptr and it will has cycle reference issue.
I try to solve this issue by reset all member shared_ptrs in class destructor.
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;
}
Run: 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
We can see there's memory leak due to cycle reference. So my idea is: I reset all member shared_ptrs in all my classes destructors. Then we have 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(nullptr) {
std::cout << name << " A::A b.get:" << b.get()
<< ", 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()
<< ", 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 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;
}
Run 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 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
We see the cycle reference is fixed!
Can I use such a design pattern to solve shared_ptr cycle reference issue in C++ project ? e.g. I just reset all the shared_ptrs in the classes destructors in a C++ project.
Aucun commentaire:
Enregistrer un commentaire