shared_ptr weak_ptr 智能指针的引用计数与内存管理

  智能指针创建对象实体时,可自动管理实体内存,在线程函数结束时 引用计数 小于等于1时 , 系统自动释放内存
         

       #include <QCoreApplication>
       #include <iostream>
       #include <memory>


       class B;  // 前向声明

       class A {
                      public:
                               std::shared_ptr<B> b_ptr;
                                A()  {
                                           std::cout << "A created" << std::endl;
                                       }
                               ~A() {
                                           std::cout << "A destroyed" << std::endl;
                                       }
                   };



      class B {
                      public:
                               std::weak_ptr<A> a_ptr;  //  weak_ptr 无循环引用问题

                                                                                     //  运行结果:

                                                                      /*   A created
                                                                            B created
                                                                            a1 ref times = 1
                                                                            b1 ref times = 2
                                                                            a1 ref times = 1
                                                                            b1 ref times = 2
                                                                            A destroyed
                                                                            B destroyed    *   /



                                     // std::shared_ptr<A>  a_ptr ; //  shared_ptr 将导致循环应用问题 

                                                                                           //  函数 shared_weak_ptr_func( ) 退出时 shared_ptr<A> 、shared_ptr<B> 引用的对象实体不会释放 

                                                                            //  运行结果: 

                                                                            /*     A  created
                                                                                    B  created
                                                                                    a1 ref times = 2
                                                                                    b1 ref times = 2
                                                                                    a1 ref times = 2
                                                                                    b1 ref times = 2    */

                                                                                                                                                      


                          B( ) {
                                     std::cout << "B created" << std::endl;
                                }


                         ~B() {
                                      std::cout << "B destroyed" << std::endl;
                                 }
                    };


        void   shared_weak_ptr_func()    //函数退出时,引用计数为1 的对象自动释放对象实体内存
                 {

                  std::shared_ptr<A> a1 = std::make_shared<A>();  // make_shared<A>() 创建的class A 对象实体被 shared_ptr a1 引用 ,其引用计数为1
                  std::shared_ptr<B> b1 = std::make_shared<B>();  // make_shared<B>() 创建的class B 对象实体被 shared_ptr b1 引用 ,其引用计数为1


                  a1->b_ptr = b1; // b1 指向的对象实体的引用计数加1
                  b1->a_ptr = a1; // a1 指向的对象实体的引用计数加1
                  //b1->a_ptr = a1; // 使用 weak_ptr 不会增加引用计数退出函数时调用析构函数  ,  使用 shared_ptr 会导致循环引用计数

                 std::cout << "a1 ref times = " <<  a1.use_count()   << std::endl; //shared_ptr 输出引用计数为 2 ,函数退出时不会调用析构函数
                 std::cout << "b1 ref times = " <<  b1.use_count()   << std::endl; //shared_ptr 输出引用计数为 2 ,函数退出时不会调用析构函数


                 std::cout << "a1 ref times = " <<  a1.use_count()   << std::endl; //weak_ptr 输出引用计数为 1  函数退出时先调用A的 析构函数 , 再调用B的析构函数
                 std::cout << "b1 ref times = " <<  b1.use_count()   << std::endl; //weak_ptr 输出引用计数为 2

              }



      int main(int argc, char *argv[])
              {
                     // QCoreApplication a(argc, argv);

                     shared_weak_ptr_func();                        

                     return 0 ;
                    // return a.exec();
              }
posted @   hunnu-TM  阅读(0)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示