• <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   管理


            伊人 久久 精品| 久久精品草草草| 日韩电影久久久被窝网| 国内精品伊人久久久久妇| 久久中文骚妇内射| 91精品国产91热久久久久福利| 9999国产精品欧美久久久久久| 午夜精品久久影院蜜桃| 色婷婷综合久久久久中文| 天天爽天天爽天天片a久久网| 女同久久| 国产一级做a爰片久久毛片| 国产精品99久久久久久宅男| 中文精品99久久国产 | 久久久久亚洲av毛片大| 中文字幕无码精品亚洲资源网久久| 国内精品久久国产大陆| 麻豆av久久av盛宴av| 国产午夜精品久久久久九九电影 | 久久精品卫校国产小美女| 久久综合狠狠色综合伊人| 无码人妻精品一区二区三区久久 | 国产精品免费久久久久久久久 | 国产精品青草久久久久福利99 | 一本色道久久88综合日韩精品 | 欧美精品久久久久久久自慰| 久久久中文字幕日本| 2020最新久久久视精品爱| 97久久精品无码一区二区| 性色欲网站人妻丰满中文久久不卡| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 麻豆一区二区99久久久久| 国产精品一区二区久久精品涩爱 | 久久人人爽人人人人爽AV| 亚洲乱码日产精品a级毛片久久| 伊人热人久久中文字幕| 岛国搬运www久久| 99久久精品国产一区二区三区| 99国产精品久久久久久久成人热| 久久国产色av免费看| 无码人妻久久一区二区三区免费丨|