简体   繁体   English

通过类析构函数中的reset成员shared_ptrs解决C++11 shared_ptr循环引用?

[英]Solve C++11 shared_ptr cycle reference through reset member shared_ptrs in class destructor?

We know that C++11 has shared_ptr and it will has cycle reference issue.我们知道 C++11 有 shared_ptr 并且它会有循环引用问题。

I try to solve this issue by reset all member shared_ptrs in class destructor.我尝试通过重置类析构函数中的所有成员 shared_ptrs 来解决此问题。

Example-1 share.cpp :示例 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 :运行: 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.所以我的想法是:我在所有类的析构函数中重置所有成员 shared_ptrs。 Then we have Example-2 share.cpp :然后我们有 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 :运行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 ?我可以使用这样的设计模式来解决 C++ 项目中的 shared_ptr 循环引用问题吗? eg I just reset all the shared_ptrs in the classes destructors in a C++ project.例如,我只是重置了 C++ 项目中类析构函数中的所有 shared_ptrs。

Short answer: No, because shared_ptr::reset() reduces the use count in the same way the shared pointer destructor does.简短回答:不,因为shared_ptr::reset()以与共享指针析构函数相同的方式减少使用计数。

Long answer: Read your assignments.长答案:阅读你的作业。

Let's take the A2/B2 case first.我们先来看 A2/B2 的情况。 After your assignment you have 2 'chains' of objects.完成任务后,您将拥有 2 个对象“链”。 B2->A1->B2 and A2->B1->A2. B2->A1->B2 和 A2->B1->A2。 The A1/B1 shared pointers have a use_count of 2 (one for the chain, one for the local variable). A1/B1 共享指针的 use_count 为 2(一个用于链,一个用于局部变量)。 The A2/B2 shared pointers have a use count of 1 (for the chain) A2/B2 共享指针的使用计数为 1(对于链)

So then you start the 3rd block那么你开始第三个块

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

Now, when this goes out of scope, A3/B3 still point to each other, and are leaked.现在,当这超出范围时,A3/B3 仍然相互指向,并且会泄漏。 The only references to the A1/B1 objects are the local variables, which then go out of scope.对 A1/B1 对象的唯一引用是局部变量,然后超出范围。

So you see the B2 and then A2 object destroyed as you perform the assignments, and then B1 and A1 are destroyed.因此,您会在执行赋值操作时看到 B2 和 A2 对象被销毁,然后 B1 和 A1 被销毁。 The A3/B3 objects are never destroyed. A3/B3 对象永远不会被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM