• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            flushthink

            just for essential skill
            隨筆 - 14, 文章 - 0, 評論 - 30, 引用 - 0
            數(shù)據(jù)加載中……

            enable_shared_from_this、weak_ptr、shared_ptr

            網(wǎng)上說weak_ptr是shared_ptr的觀察員,weak_ptr不會干擾shared_ptr機(jī)制,當(dāng)weak_ptr所觀察的shared_ptr要釋放對象時,weak_ptr的指針將被置空,避免空懸指針。
            weak_ptr只能通過shared_ptr或weak_ptr構(gòu)造。

            對于一個shared_ptr,它分別對強(qiáng)引用和弱引用都做了計數(shù)。

            上圖是下面代碼的調(diào)試信息。
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            struct A
            {};

            int _tmain(int argc, _TCHAR* argv[])
            {
                boost::shared_ptr
            <A> a(new A);

                
            return 0;
            }

            當(dāng)一個shared_ptr構(gòu)造,use_count_和weak_count_都被賦值為1。



            上圖是下面代碼調(diào)試信息。
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            struct A
            {};

            class B 
            {
            public:
                B()
                    : mA(
            new A)
                
            {
                }


                
            /// 把指針返回出去
                boost::shared_ptr<A> get()
                
            {
                    
            return mA;
                }

            private:
                boost::shared_ptr
            <A> mA;
            }
            ;

            int _tmain(int argc, _TCHAR* argv[])
            {
                
            {
                    B b;
                    boost::weak_ptr
            <A> wp1 = b.get();
                    boost::weak_ptr
            <A> wp2 = b.get();
                    boost::shared_ptr
            <A> sp1 = b.get();
                    boost::shared_ptr
            <A> sp2 = b.get();
                }


                
            return 0;
            }



            當(dāng)一個對象返回出一個自身對象shared_ptr,一般用enable_shared_from_this,而enable_shared_from_this就是用weak_ptr來實(shí)現(xiàn)的。

             

            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            struct A : public boost::enable_shared_from_this<A>
            {

                boost::shared_ptr
            <A> get()
                
            {
                    boost::shared_ptr
            <A> holder(new A);
                    
            return holder;
                }

            }
            ;

            int _tmain(int argc, _TCHAR* argv[])
            {
                
                A a;
                boost::shared_ptr
            <A> aa = a.get();
                
            return 0;
            }

            上述代碼的對象構(gòu)造是:enable_shared_from_this<A>,然后是A,再就是shared_ptr,所以必須在構(gòu)造完成之后再進(jìn)行對象智能指針的創(chuàng)建。這是一種循環(huán)依賴關(guān)系,一般循環(huán)依賴都要用到weak_ptr。

            針對循環(huán)依賴的shared_ptr如下:
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            /// 相互依賴
            struct Product;
            struct ProductManager;

            typedef boost::shared_ptr
            <Product> ProductPtr;
            typedef boost::shared_ptr
            <ProductManager> ProductManagerPtr;

            struct Product
            {
                Product()
                
            {
                    std::cout 
            << "Product 構(gòu)造" << std::endl;
                }

                
            ~Product()
                
            {
                    std::cout 
            << "Product 析構(gòu)" << std::endl;
                }

                ProductManagerPtr mMgr;
            }
            ;

            struct ProductManager
            {
                ProductManager()
                
            {
                    std::cout 
            << "ProductManager 構(gòu)造" << std::endl;
                }

                
            ~ProductManager()
                
            {
                    std::cout 
            << "ProductManager 析構(gòu)" << std::endl;
                }

                ProductPtr mProduct;
            }
            ;

            int _tmain(int argc, _TCHAR* argv[])
            {
                ProductPtr product(
            new Product);
                ProductManagerPtr productMgr(
            new ProductManager);
                product
            ->mMgr = productMgr;
                productMgr
            ->mProduct = product;
                
            return 0;
            }

            結(jié)果是:


            不能釋放對象。
            對于這種情況要這么做:

            struct Product
            {
                Product()
                
            {
                    std::cout 
            << "Product 構(gòu)造" << std::endl;
                }

                
            ~Product()
                
            {
                    std::cout 
            << "Product 析構(gòu)" << std::endl;
                }

                boost::weak_ptr
            <ProductManager> mMgr;
            }
            ;


             

            posted on 2009-09-17 15:14 tiny 閱讀(2125) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久综合国产乱子伦精品免费| 亚洲精品WWW久久久久久| 久久久久亚洲精品日久生情 | 久久久久久a亚洲欧洲aⅴ| 亚洲国产成人久久综合碰碰动漫3d| 国产精自产拍久久久久久蜜| 久久91精品国产91久| 国产亚洲欧美精品久久久| 成人亚洲欧美久久久久| 亚洲精品久久久www| 久久精品亚洲中文字幕无码麻豆| 日韩va亚洲va欧美va久久| 波多野结衣中文字幕久久| 久久精品国产亚洲Aⅴ蜜臀色欲| 久久国产亚洲精品无码| 久久国产精品无码网站| 伊人久久大香线蕉亚洲五月天| 久久99精品久久久久久9蜜桃| 国产一区二区精品久久| 亚洲精品乱码久久久久久自慰| 亚洲伊人久久成综合人影院| 99久久成人国产精品免费| 久久久噜噜噜久久中文福利| 办公室久久精品| 国产成人精品久久一区二区三区 | 亚洲日本va午夜中文字幕久久| 国内精品久久人妻互换| 亚洲午夜久久久久久久久电影网| 久久91这里精品国产2020| 久久久精品午夜免费不卡| 精品久久一区二区三区| 色诱久久久久综合网ywww| 亚洲欧美伊人久久综合一区二区 | 新狼窝色AV性久久久久久| 伊人久久大香线蕉综合5g| 欧美激情精品久久久久久久九九九| 国产精品综合久久第一页| 青草影院天堂男人久久| 国产日韩久久久精品影院首页| 亚洲国产精品久久久久婷婷老年| 99久久99久久精品免费看蜜桃 |