• <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, 評(píng)論 - 30, 引用 - 0
            數(shù)據(jù)加載中……

            enable_shared_from_this、weak_ptr、shared_ptr

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

            對(duì)于一個(gè)shared_ptr,它分別對(duì)強(qiáng)引用和弱引用都做了計(jì)數(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)一個(gè)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)
                
            {
                }


                
            /// 把指針?lè)祷爻鋈?/span>
                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)一個(gè)對(duì)象返回出一個(gè)自身對(duì)象shared_ptr,一般用enable_shared_from_this,而enable_shared_from_this就是用weak_ptr來(lái)實(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;
            }

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

            針對(duì)循環(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é)果是:


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

            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 閱讀(2132) 評(píng)論(0)  編輯 收藏 引用


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


            日韩十八禁一区二区久久| 精品久久久久久成人AV| 久久久国产打桩机| 欧美久久精品一级c片片| 亚洲国产精品18久久久久久| 久久一区二区三区免费| 99久久精品无码一区二区毛片| 狠狠色婷婷久久一区二区三区| 亚洲欧美一级久久精品| 亚洲国产精品无码久久久久久曰 | 精品国产VA久久久久久久冰| 99久久无色码中文字幕人妻| 亚洲AV无码久久精品成人| 国产精品一区二区久久精品涩爱| 久久人人爽人人爽人人片AV不| 亚洲日韩欧美一区久久久久我| 性做久久久久久免费观看| 色婷婷噜噜久久国产精品12p| 无码任你躁久久久久久久| 久久综合九色欧美综合狠狠| 亚洲国产成人精品女人久久久 | 性欧美大战久久久久久久| 亚洲人成无码网站久久99热国产| 免费一级做a爰片久久毛片潮| 91秦先生久久久久久久| 狠狠色丁香婷婷久久综合| 久久精品国产日本波多野结衣| 亚洲综合日韩久久成人AV| 久久精品国产亚洲AV香蕉| 久久久无码精品午夜| 久久婷婷五月综合色奶水99啪| 波多野结衣久久| 97精品伊人久久久大香线蕉 | 精品人妻伦一二三区久久| 无码人妻少妇久久中文字幕蜜桃| 久久久久成人精品无码中文字幕 | 久久99国产精品成人欧美| 99久久国产亚洲综合精品| 国产精品久久国产精品99盘| 亚洲AV无码久久精品蜜桃| 国产精品伊人久久伊人电影 |