• 11、weak_ptr


      弱引用智能指针 std::weak_ptr 可以看做是 shared_ptr 的助手,它不管理 shared_ptr 内部的指针。std::weak_ptr 没有重载操作符 * 和 ->,因为它不共享指针,不能操作资源,所以它的构造不会增加引用计数,析构也不会减少引用计数,它的主要作用就是作为一个旁观者监视 shared_ptr 中管理的资源是否存在

    一、weak_ptr的2作用

    (1)返回管理 this 的 shared_ptr

      如果在一个类中编写了一个函数,通过这个得到管理当前对象的共享智能指针,我们可能会写出如下代码:

    #include <iostream>
    #include <memory>
    using namespace std;
    
    struct Test
    {
        shared_ptr<Test> getSharedPtr()
        {
            return shared_ptr<Test>(this);
        }
        
        ~Test()
        {
            cout << "class Test is disstruct ..." << endl;
        }
    
    };
    
    int main() 
    {
        shared_ptr<Test> sp1(new Test);
        cout << "use_count: " << sp1.use_count() << endl;
        shared_ptr<Test> sp2 = sp1->getSharedPtr();
        cout << "use_count: " << sp1.use_count() << endl;
        return 0;
    }

    执行上面的测试代码,运行中会出现异常,在终端还是能看到对应的日志输出:

    use_count: 1
    use_count: 1
    class Test is disstruct ...
    class Test is disstruct ...

      通过输出的结果可以看到一个对象被析构了两次,其原因是这样的:在这个例子中使用同一个指针 this 构造了两个智能指针对象 sp1 和 sp2,这二者之间是没有任何关系的,因为 sp2 并不是通过 sp1 初始化得到的实例对象。在离开作用域之后 this 将被构造的两个智能指针各自析构,导致重复析构的错误。

    这个问题可以通过 weak_ptr 来解决,通过 wek_ptr 返回管理 this 资源的共享智能指针对象 shared_ptr。C++11 中为我们提供了一个模板类叫做 std::enable_shared_from_this<T>,这个类中有一个方法叫做 shared_from_this(),通过这个方法可以返回一个共享智能指针,在函数的内部就是使用 weak_ptr 来监测 this 对象,并通过调用 weak_ptr 的 lock() 方法返回一个 shared_ptr 对象。

    修改之后的代码为:

    #include <iostream>
    #include <memory>
    using namespace std;
    
    struct Test : public enable_shared_from_this<Test>
    {
        shared_ptr<Test> getSharedPtr()
        {
            return shared_from_this();
        }
        ~Test()
        {
            cout << "class Test is disstruct ..." << endl;
        }
    };
    
    int main() 
    {
        shared_ptr<Test> sp1(new Test);
        cout << "use_count: " << sp1.use_count() << endl;
        shared_ptr<Test> sp2 = sp1->getSharedPtr();
        cout << "use_count: " << sp1.use_count() << endl;
        return 0;
    }
    use_count: 1
    use_count: 2
    class Test is disstruct ...

      最后需要强调一个细节:在调用 enable_shared_from_this 类的 hared_from_this () 方法之前,必须要先初始化函数内部 weak_ptr 对象,否则该函数无法返回一个有效的 shared_ptr 对象(具体处理方法可以参考上面的示例代码)。

      (2) 解决循环引用问题

      智能指针如果循环引用会导致内存泄露,比如下面的例子:

    #include <iostream>
    #include <memory>
    using namespace std;
    
    struct TA;
    struct TB;
    
    struct TA
    {
        shared_ptr<TB> bptr;
        ~TA()
        {
            cout << "class TA is disstruct ..." << endl;
        }
    };
    
    struct TB
    {
        shared_ptr<TA> aptr;
        ~TB()
        {
            cout << "class TB is disstruct ..." << endl;
        }
    };
    
    void testPtr()
    {
        shared_ptr<TA> ap(new TA);
        shared_ptr<TB> bp(new TB);
        cout << "TA object use_count: " << ap.use_count() << endl;
        cout << "TB object use_count: " << bp.use_count() << endl;
    
        ap->bptr = bp;
        bp->aptr = ap;
        cout << "TA object use_count: " << ap.use_count() << endl;
        cout << "TB object use_count: " << bp.use_count() << endl;
    }
    
    int main()
    {
        testPtr();
        return 0;
    }

    测试程序输出的结果如下:

    TA object use_count: 1
    TB object use_count: 1
    TA object use_count: 2
    TB object use_count: 2

      在测试程序中,共享智能指针 ap、bp 对 TA、TB 实例对象的引用计数变为 2,在共享智能指针离开作用域之后引用计数只能减为1,这种情况下不会去删除智能指针管理的内存,导致类 TA、TB 的实例对象不能被析构,最终造成内存泄露。通过使用 weak_ptr 可以解决这个问题,只要将类 TA 或者 TB 的任意一个成员改为 weak_ptr,修改之后的代码如下:

    #include <iostream>
    #include <memory>
    using namespace std;
    
    struct TA;
    struct TB;
    
    struct TA
    {
        weak_ptr<TB> bptr;
        ~TA()
        {
            cout << "class TA is disstruct ..." << endl;
        }
    };
    
    struct TB
    {
        shared_ptr<TA> aptr;
        ~TB()
        {
            cout << "class TB is disstruct ..." << endl;
        }
    };
    
    void testPtr()
    {
        shared_ptr<TA> ap(new TA);
        shared_ptr<TB> bp(new TB);
        cout << "TA object use_count: " << ap.use_count() << endl;
        cout << "TB object use_count: " << bp.use_count() << endl;
    
        ap->bptr = bp;
        bp->aptr = ap;
        cout << "TA object use_count: " << ap.use_count() << endl;
        cout << "TB object use_count: " << bp.use_count() << endl;
    }
    
    int main()
    {
        testPtr();
        return 0;
    }

    程序输出的结果:

    TA object use_count: 1
    TB object use_count: 1
    TA object use_count: 2
    TB object use_count: 1
    class TB is disstruct ...
    class TA is disstruct ...

    通过输出的结果可以看到类 TA 或者 TB 的对象被成功析构了。

      上面程序中,在对类 TA 成员赋值时 ap->bptr = bp; 由于 bptr 是 weak_ptr 类型,这个赋值操作并不会增加引用计数,所以 bp 的引用计数仍然为 1,在离开作用域之后 bp 的引用计数减为 0,类 TB 的实例对象被析构。

    在类 TB 的实例对象被析构的时候,内部的 aptr 也被析构,其对 TA 对象的管理解除,内存的引用计数减为 1,当共享智能指针 ap 离开作用域之后,对 TA 对象的管理也解除了,内存的引用计数减为 0,类 TA 的实例对象被析构。

    二、初始化

    (1)构造函数来完成初始化

    #include <iostream>
    #include <memory>
    using namespace std;
    
    int main() 
    {
        shared_ptr<int> sp(new int);
    
        weak_ptr<int> wp1;
        weak_ptr<int> wp2(wp1);
        weak_ptr<int> wp3(sp);
        weak_ptr<int> wp4;
        wp4 = sp;
        weak_ptr<int> wp5;
        wp5 = wp3;
        
        return 0;
    }
    weak_ptr<int> wp1; 构造了一个空 weak_ptr 对象
    weak_ptr<int> wp2(wp1); 通过一个空 weak_ptr 对象构造了另一个空 weak_ptr 对象
    weak_ptr<int> wp3(sp); 通过一个 shared_ptr 对象构造了一个可用的 weak_ptr 实例对象
    wp4 = sp; 通过一个 shared_ptr 对象构造了一个可用的 weak_ptr 实例对象(这是一个隐式类型转换)
    wp5 = wp3; 通过一个 weak_ptr 对象构造了一个可用的 weak_ptr 实例对象

    三、其他常用方法

     1 use_count()

      通过调用 std::weak_ptr 类提供的 use_count() 方法可以获得当前所观测资源的引用计数

    #include <iostream>
    #include <memory>
    using namespace std;
    
    int main() 
    {
        shared_ptr<int> sp(new int);
    
        weak_ptr<int> wp1;
        weak_ptr<int> wp2(wp1);
        weak_ptr<int> wp3(sp);
        weak_ptr<int> wp4;
        wp4 = sp;
        weak_ptr<int> wp5;
        wp5 = wp3;
    
        cout << "use_count: " << endl;
        cout << "wp1: " << wp1.use_count() << endl;
        cout << "wp2: " << wp2.use_count() << endl;
        cout << "wp3: " << wp3.use_count() << endl;
        cout << "wp4: " << wp4.use_count() << endl;
        cout << "wp5: " << wp5.use_count() << endl;
        return 0;
    }
    use_count:
    wp1: 0
    wp2: 0
    wp3: 1
    wp4: 1
    wp5: 1

    2、expired()

      通过调用 std::weak_ptr 类提供的 expired() 方法来判断观测的资源是否已经被释放

    #include <iostream>
    #include <memory>
    using namespace std;
    
    int main() 
    {
        shared_ptr<int> shared(new int(10));
        weak_ptr<int> weak(shared);
        cout << "1. weak " << (weak.expired() ? "is" : "is not") << " expired" << endl;
    
        shared.reset();
        cout << "2. weak " << (weak.expired() ? "is" : "is not") << " expired" << endl;
    
        return 0;
    }
    weak is not expired
    weak is expired

      weak_ptr 监测的就是 shared_ptr 管理的资源,当共享智能指针调用 shared.reset(); 之后管理的资源被释放,因此 weak.expired() 函数的结果返回 true,表示监测的资源已经不存在了。

    3、lock()

      通过调用 std::weak_ptr 类提供的 lock() 方法来获取管理所监测资源的 shared_ptr 对象

    #include <iostream>
    #include <memory>
    using namespace std;
    
    int main()
    {
        shared_ptr<int> sp1, sp2;
        weak_ptr<int> wp;
    
        sp1 = std::make_shared<int>(520);
        wp = sp1;
        sp2 = wp.lock();
        cout << "use_count: " << wp.use_count() << endl;
    
        sp1.reset();
        cout << "use_count: " << wp.use_count() << endl;
    
        sp1 = wp.lock();
        cout << "use_count: " << wp.use_count() << endl;
    
        cout << "*sp1: " << *sp1 << endl;
        cout << "*sp2: " << *sp2 << endl;
    
        return 0;
    }
    use_count: 2
    use_count: 1
    use_count: 2
    *sp1: 520
    *sp2: 520
    111
  • 相关阅读:
    解决Android SDK Manager无法更新下载
    使用Anaconda3配置多版本Python虚拟开发环境
    Python·Jupyter Notebook各种使用方法
    学习 python 编写规范 pep8 的问题笔记
    ajax工作原理及其优缺点
    json和jsonp
    cookie、session、localStorage、sessionStorage区别
    浅谈前端性能优化(PC版)
    浅谈前端性能优化(移动端)
    前端优化 -- Combo Handler
  • 原文地址:https://www.cnblogs.com/zwj-199306231519/p/15143741.html
Copyright © 2020-2023  润新知