通过在class析构函数中重置成员shared_ptrs解决C++11shared_ptr循环引用?

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

我们知道 C++11 有 shared_ptr 并且会有循环引用问题。

我尝试通过重置 class 析构函数中的所有成员 shared_ptr 来解决这个问题。

示例 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

我们可以看到由于循环引用导致内存泄漏。所以我的想法是:我在所有 classes 析构函数中重置所有成员 shared_ptrs。然后我们有示例 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;
}

运行 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

我们看到循环参考是固定的!

我可以使用这样的设计模式来解决 C++ 项目中的 shared_ptr 循环引用问题吗?例如我只是重置了 C++ 项目中 classes 析构函数中的所有 shared_ptr。

简短回答:不,因为 shared_ptr::reset() 以与共享指针析构函数相同的方式减少使用次数。

长答案:阅读你的作业。

我们先来看A2/B2这个案例。分配后,您有 2 'chains' 个对象。 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 对象永远不会被销毁。