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

            Error

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              217 Posts :: 61 Stories :: 32 Comments :: 0 Trackbacks
            // boost_singal_solt.cpp : Defines the entry point for the console application.
            //
            #include "stdafx.h"
            #include <iostream>
            #include <string>
            #include <boost/signals2.hpp>
            class CTarget /*: public boost::signals2::trackable*/
            {
            public:
                virtual ~CTarget()
                {
                    std::cout << __FUNCTION__ << std::endl;
                }
                CTarget()
                {
                    m_strMsg = "1343rwkjdfkjdfasdklf";
                }
                void SomeHandle()
                {
                    std::cout << __FUNCTION__ << std::endl;
                    std::cout << m_strMsg << std::endl;
                }
                void SomeHandle1()
                {
                    std::cout << __FUNCTION__ << std::endl;
                }
            private:
                std::string m_strMsg;
            };
            class CSource
            {
            public:
                boost::signals2::connection Connect(const boost::signals2::signal<void(void)>::slot_type &sub)
                {
                    return m_sig.connect(sub);
                }
                void OnSig()
                {
                    m_sig();
                }
            private:
                boost::signals2::signal<void(void)> m_sig;
            };
            int _tmain(int argc, _TCHAR* argv[])
            {
                CSource source;
                boost::signals2::connection conn;
                {
                    CTarget target;
                    
                    /*conn = */source.Connect(boost::bind(&CTarget::SomeHandle, &target));
                    source.Connect(boost::bind(&CTarget::SomeHandle1, &target));
                    source.OnSig();
                }
                source.OnSig();
            return 0;
            }


            在繼承boost::signals2::trackable的情況下,Target析構(gòu)以后不會調(diào)用,但是不繼承是會調(diào)用的。也就是說,這是一個危險操作,不知道內(nèi)部怎么管理的,會不會出現(xiàn)內(nèi)存泄漏?
            /////////////////////////////////////////////////////////////////////////////


            // boost_singal_solt.cpp : Defines the entry point for the console application.
            //
            #include "stdafx.h"
            #include <iostream>
            #include <string>
            #include <boost/signals2.hpp>
            class CSource
            {
            public:
                boost::signals2::connection Connect(const boost::signals2::signal<void(void)>::slot_type &sub)
                {
                    return m_sig.connect(sub);
                }
                void OnSig()
                {
                    m_sig();
                }
            private:
                boost::signals2::signal<void(void)> m_sig;
            };
            class CTarget : public boost::signals2::trackable
            {
            public:
                virtual ~CTarget()
                {
                    std::cout << __FUNCTION__ << std::endl;
                    m_con.disconnect();
                }
                CTarget()
                {
                    m_strMsg = "1343rwkjdfkjdfasdklf";
                }
                void RegisterSolts(CSource& source)
                {
                    m_con = source.Connect(boost::bind(&CTarget::SomeHandle, this));
                }
                void SomeHandle()
                {
                    std::cout << __FUNCTION__ << std::endl;
                    std::cout << m_strMsg << std::endl;
                }
                void SomeHandle1()
                {
                    std::cout << __FUNCTION__ << std::endl;
                }
            private:
                std::string m_strMsg;
                boost::signals2::connection m_con;
            };
            int _tmain(int argc, _TCHAR* argv[])
            {
                CSource source;
                boost::signals2::connection conn;
                {
                    CTarget target;
                    
                    target.RegisterSolts(source);
                    //source.OnSig();
                }
                source.OnSig();
            return 0;
            }
            確認(rèn)只要維護好connection在對象target析構(gòu)以后一定是disconnect應(yīng)該就OK沒事,,,





            ///////////////////////////////////////////


            整體上看來是這樣:
            問題:target的成員函數(shù)和this指針打包注冊到source上以后,如果target析構(gòu),source無法感知到,可能會調(diào)用已經(jīng)成為野指針的target地址。
            解決:signals2庫的解決辦法是,用一個connection來維護打包的target成員函數(shù)和source上的signal的連接關(guān)系。target持有這個connection,當(dāng)自身析構(gòu)的時候必須調(diào)用disscontion解除這個關(guān)系。

            如果要抄襲,也是要設(shè)計一種機制,在target析構(gòu)的時候,想辦法清除它在別處注冊的函數(shù)指針。
            1.可以建立target到source的關(guān)系,target析構(gòu)以后通知source,souce清理自身保留的target相關(guān)打包函數(shù)。這種方式在使用boost bind的時候可能就有問題了,應(yīng)為之依靠target指針去判斷bind出來的function有難度。
            2.建立關(guān)系的時候就將解除關(guān)系的函數(shù)打包到一個新的對象中,然后由target負(fù)責(zé)在自己析構(gòu)的時候去調(diào)用它。

            !!!如果source先于target析構(gòu),就悲劇了。但是實際情況是,如果source已經(jīng)析構(gòu)了,也無法觸發(fā)target綁定的函數(shù)了。但問題是,target析構(gòu)的時候還是去觸發(fā)source綁定到target的解除關(guān)系的函數(shù)。悲催。但是實際測試這個問題不存在,估計是用類似weak_ptr的技術(shù)。
            在完全理解之前,務(wù)必保證source生命周期大于target
            // boost_singal_solt.cpp : Defines the entry point for the console application.
            //
            #include "stdafx.h"
            #include <iostream>
            #include <string>
            #include <boost/signals2.hpp>
            class CSource
            {
            public:
                boost::signals2::connection Connect(const boost::signals2::signal<void(void)>::slot_type &sub)
                {
                    return m_sig.connect(sub);
                }
                void OnSig()
                {
                    m_sig();
                }
            private:
                boost::signals2::signal<void(void)> m_sig;
            };
            class CTarget : public boost::signals2::trackable
            {
            public:
                virtual ~CTarget()
                {
                    //std::cout << __FUNCTION__ << std::endl;
                    m_con.disconnect();
                }
                CTarget()
                {
                    m_strMsg = "1343rwkjdfkjdfasdklf";
                }
                void RegisterSolts(CSource& source)
                {
                    m_con = source.Connect(boost::bind(&CTarget::SomeHandle, this));
                }
                void SomeHandle()
                {
                    std::cout << __FUNCTION__ << std::endl;
                    std::cout << m_strMsg << std::endl;
                }
                void SomeHandle1()
                {
                    std::cout << __FUNCTION__ << std::endl;
                }
            private:
                std::string m_strMsg;
                boost::signals2::connection m_con;
            };
            int _tmain(int argc, _TCHAR* argv[])
            {
                for (;;)
                {
                    CSource* pSource = new CSource;
                    boost::signals2::connection conn;
                    CTarget* pTarget = new CTarget;
                    pTarget->RegisterSolts(*pSource);
                    delete pSource;
                    delete pTarget;
                }
                //source.OnSig();
            return 0;
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            沒有完全讀明白之前,在需要7×24運行的環(huán)境還是慎重使用。不過客戶端貌似無所謂,,,囧。
            posted on 2014-05-30 01:09 Enic 閱讀(791) 評論(0)  編輯 收藏 引用 所屬分類: boost
            久久精品一区二区影院 | 99久久777色| 久久天天躁狠狠躁夜夜躁2O2O| 久久精品中文字幕无码绿巨人| 久久人妻少妇嫩草AV无码专区| 国产精品毛片久久久久久久| 欧美日韩精品久久久免费观看| 久久久久久久精品妇女99| 久久综合久久自在自线精品自| 一本久久久久久久| 97精品国产97久久久久久免费 | 久久精品国产亚洲AV影院| 亚洲国产一成人久久精品| 久久噜噜电影你懂的| 伊人久久成人成综合网222| 9久久9久久精品| 亚洲性久久久影院| 久久久久国产精品| 无码人妻久久一区二区三区免费丨 | 精品伊人久久久| 72种姿势欧美久久久久大黄蕉| 久久亚洲精品无码观看不卡| 精品乱码久久久久久久| 无码任你躁久久久久久老妇| 777久久精品一区二区三区无码| 国产成人久久精品一区二区三区| 国产精自产拍久久久久久蜜 | 91精品国产色综久久| 久久精品国产亚洲AV不卡| 久久精品国产亚洲7777| 亚洲天堂久久精品| 99久久免费国产精精品| 亚洲色大成网站www久久九| 伊人久久精品影院| 亚洲AV伊人久久青青草原| 久久久久亚洲精品天堂久久久久久 | 国产精品久久久久天天影视| 久久夜色精品国产欧美乱| 久久天天躁夜夜躁狠狠躁2022| 久久久久久久免费视频| 久久久久久久91精品免费观看|