C++ 求解C++;11通过类析构函数中的重置成员共享\u ptr进行共享\u ptr循环引用?

C++ 求解C++;11通过类析构函数中的重置成员共享\u ptr进行共享\u ptr循环引用?,c++,c++11,memory,memory-leaks,shared-ptr,C++,C++11,Memory,Memory Leaks,Shared Ptr,我们知道C++11已经共享了_ptr,它将有循环引用问题 我试图通过在类析构函数中重置所有成员共享的ptr来解决这个问题 示例1share.cpp: #include <iostream> #include <memory> #include <string> class A; class B; struct A { A(const std::string &a_name) : name(a_name), b(nullptr) { st

我们知道C++11已经共享了_ptr,它将有循环引用问题

我试图通过在类析构函数中重置所有成员共享的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;
}
我们可以看到由于循环引用而导致内存泄漏。所以我的想法是:重置所有类析构函数中的所有成员共享的\u ptr。然后是示例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;
}
我们看到循环参考是固定的


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

< P>简短回答:不,因为<代码> SyddyPt::ReStET()/Cuff>以共享指针析构函数的方式减少使用计数。

长答案:阅读你的作业

让我们先来看A2/B2案例。分配后,您有两个对象“链”。B2->A1->B2和A2->B1->A2。A1/B1共享指针的use_计数为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对象永远不会被销毁。

当存在引用循环时,析构函数将永远无法运行,因此更改析构函数中的代码对您没有帮助。更好的解决方案是确定哪些指针是“反向”指针(例如,A->b->A循环中的b->A),并将它们设置为
weak_ptr
类型,甚至是常规的C型指针,这样它们就不会增加引用计数。没错,
a3
b3
析构函数消息不会打印到控制台,因此,在离开作用域时,它们的析构函数永远不会被调用。内存泄漏。
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
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