东方耀AI技术分享

标题: 问题抛出:智能指针shared_ptr用weak_ptr打破循环引用 [打印本页]

作者: 东方耀    时间: 2021-9-14 18:08
标题: 问题抛出:智能指针shared_ptr用weak_ptr打破循环引用







问题抛出:智能指针shared_ptr用weak_ptr打破循环引用


  1. #include <iostream>
  2. #include <memory>

  3. class Parent;   // 前置申明
  4. typedef std::shared_ptr<Parent> ParentPtr;

  5. class Child{
  6.     public:
  7.         ParentPtr father;
  8.         Child();
  9.         ~Child();
  10. };

  11. typedef std::shared_ptr<Child> ChildPtr;
  12. class Parent{
  13.     public:
  14.         ChildPtr son;
  15.         Parent();
  16.         ~Parent();
  17. };

  18. Child::Child(){
  19.     printf("hi child\n");
  20. }

  21. Parent::Parent(){
  22.     printf("hi parent\n");
  23. }

  24. Child::~Child(){
  25.     printf("bye child\n");
  26. }

  27. Parent::~Parent(){
  28.     printf("bye parent\n");
  29. }

  30. void test_parent_and_child(){
  31.     ParentPtr p(new Parent());
  32.     ChildPtr c(new Child());
  33.     printf("use_count()=%ld,%ld\n", p.use_count(), c.use_count());
  34.     // 问题出在:下面2行代码中 没有析构 内存泄露
  35.     p->son = c;
  36.     printf("循环指use_count()=%ld,%ld\n", p.use_count(), c.use_count());
  37.     c->father = p;
  38.     printf("循环指use_count()=%ld,%ld\n", p.use_count(), c.use_count());

  39.     // 该语句块结束后 会自动析构 导致use_count会减1 最终引用数都是1 相互等待 进入死循环 导致对象不析构

  40. }



  41. int main(){
  42.     printf("问题抛出:智能指针shared_ptr用weak_ptr打破循环引用.cpp\n");
  43.     test_parent_and_child();
  44.     // shared_ptr_with_weak_ptr();
  45.     return 0;
  46. }
复制代码



作者: 东方耀    时间: 2021-9-15 09:47
参考这个用法:http://www.ai111.vip/thread-1261-1-1.html
改进如下:
  1. #include <iostream>
  2. #include <memory>

  3. class Parent;   // 前置申明
  4. //weak_ptr只能与shared_ptr结合使用,不能单独使用
  5. typedef std::shared_ptr<Parent> ParentPtr;
  6. typedef std::weak_ptr<Parent> ParentPtr_weak;


  7. class Child{
  8.     public:
  9.         ParentPtr_weak father;  //只用一个即可 打破了循环引用
  10.         Child();
  11.         ~Child();
  12. };

  13. typedef std::shared_ptr<Child> ChildPtr;
  14. typedef std::weak_ptr<Child> ChildPtr_weak;
  15. class Parent{
  16.     public:
  17.         ChildPtr_weak son;    // 两个都改 更符合对象析构的顺序吧
  18.         Parent();
  19.         ~Parent();
  20. };

  21. Child::Child(){
  22.     printf("hi child\n");
  23. }

  24. Parent::Parent(){
  25.     printf("hi parent\n");
  26. }

  27. Child::~Child(){
  28.     printf("bye child\n");
  29. }

  30. Parent::~Parent(){
  31.     printf("bye parent\n");
  32. }

  33. void test_parent_and_child(){
  34.     ParentPtr p(new Parent());
  35.     ChildPtr c(new Child());
  36.     printf("use_count()=%ld,%ld\n", p.use_count(), c.use_count());
  37.     // 问题出在:下面2行代码中 没有析构 内存泄露
  38.     p->son = c;  // 又是用强指针赋值给弱指针
  39.     //Object_weak_ptr weak_obj(obj); //一般通过shared_ptr来构建weak_ptr
  40.     printf("循环指use_count()=%ld,%ld\n", p.use_count(), c.use_count());
  41.     c->father = p;
  42.     printf("循环指use_count()=%ld,%ld\n", p.use_count(), c.use_count());

  43.     // 该语句块结束后 会自动析构 导致use_count会减1 最终引用数都是1 相互等待 进入死循环 导致对象不析构

  44. }



  45. int main(){
  46.     printf("问题抛出:智能指针shared_ptr用weak_ptr打破循环引用.cpp\n");
  47.     test_parent_and_child();
  48.     // shared_ptr_with_weak_ptr();
  49.     return 0;
  50. }
复制代码

作者: zouqiqi    时间: 2021-11-23 19:28
让天下人人学会人工智能!人工智能的前景一片大好!




欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/) Powered by Discuz! X3.4