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

            C++研究

            C++細節深度探索及軟件工程

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              37 隨筆 :: 0 文章 :: 74 評論 :: 0 Trackbacks
            Memory leaks , more important in Symbian C++ than that's in the PCS, But when you programs for a Media Processor which must run for there year or even much longer , you must consider Memory leaks seriously. How can avoid leaking?
             
            1. remeber delete after you new operation
             
            Just this is enougn? Of course , Many leaks come from this stimulation. but that's not all.
            for E.G:
            class ClassB {
            private:
            ClassA* ptr1; //pointer members
            ClassA* ptr2;
            public:
            //constructor that initializes the pointers
            //- will cause resource leak if second new throws
            ClassB (ClassA val1, ClassA val2)
            : ptr1(new ClassA(val1)), ptr2(new ClassA(val2)) {
            }
            //copy constructor
            //- might cause resource leak if second new throws
            ClassB (const ClassB& x)
            : ptr1(new ClassA(*x.ptr1)), ptr2(new ClassA(*x.ptr2)) {
            }
            //assignment operator
            const ClassB& operator= (const ClassB& x) {
            *ptr1 = *x.ptr1;
            *ptr2 = *x.ptr2;
            return *this;
            }
            ~ClassB () {
            delete ptr1;
            delete ptr2;
            }
            ...
            };
            if an exception occurs inside a constructor,
            destructors are only called for objects that have been fully constructed. This might result in a
            resource leak if, for example, the first new was successful but the second was not.
             
            2.How can I avoid such a possible resource leak?
            you can use simply auto_ptr.(In Symbian , in my opinion ,The CleanStack works in the same machanism)
             
            The code can write like the follows:
            class ClassB {
            private:
            const std::auto_ptr<ClassA> ptr1; //auto_ptr members
            const std::auto_ptr<ClassA> ptr2;
            public:
            //constructor that initializes the auto_ptrs
            //- no resource leak possible
            ClassB (ClassA val1, ClassA val2)
            : ptr1 (new ClassA(val1)), ptr2(new ClassA(val2)) {
            }
            //copy constructor
            //- no resource leak possible
            ClassB (const ClassB& x)
            : ptr1(new ClassA(*x.ptr1), ptr2(new ClassA(*x.ptr2)) {
            }
            const ClassB& operator= (const ClassB& x) {
            *ptr1 = *x.ptr1;
            *ptr2 = *x.ptr2;
            return *this;
            }
            //no destructor necessary
            //(default destructor lets ptr1 and ptr2 delete their objects)
            ...
            };
             
            Note, however, that although you can skip the destructor, you still have to program the copy
            constructor and the assignment operator. By default, both would try to transfer ownership, which
            is probably not the intention. In addition, and as mentioned on page 42, to avoid an unintended
            transfer of ownership you should also use constant auto_ptrs here if the auto_ptr should
            refer to the same object throughout its lifetime
            posted on 2007-04-16 22:22 常興龍 閱讀(200) 評論(0)  編輯 收藏 引用
            > hi的博客
            久久国产精品99精品国产987| 久久亚洲高清综合| 精品久久久久久无码中文字幕一区| 久久婷婷五月综合色高清| 国产成人精品久久一区二区三区 | 亚洲美日韩Av中文字幕无码久久久妻妇 | 久久久久久久综合综合狠狠| 亚洲а∨天堂久久精品9966| 亚洲中文精品久久久久久不卡| 91精品国产综合久久婷婷| 欧美麻豆久久久久久中文| 欧美一区二区三区久久综| 理论片午午伦夜理片久久 | 色播久久人人爽人人爽人人片aV| 狠狠色狠狠色综合久久| 99久久精品无码一区二区毛片| 2021国内久久精品| 国产精品成人99久久久久| 人妻精品久久无码区| 中文字幕无码久久精品青草 | 亚洲国产精久久久久久久| 国产亚洲精品久久久久秋霞 | 亚洲午夜无码AV毛片久久| 青青国产成人久久91网| 日本人妻丰满熟妇久久久久久| 亚洲国产精品无码久久九九| 婷婷综合久久中文字幕| 99re这里只有精品热久久| 狼狼综合久久久久综合网| 国产精品久久久久久久久久影院| 久久久九九有精品国产| 久久福利青草精品资源站免费 | 国产精品久久久久久久久久免费| 久久久久亚洲精品天堂| 久久人妻少妇嫩草AV无码专区| 亚洲综合伊人久久综合| 亚洲愉拍99热成人精品热久久| 一本久久精品一区二区| 久久综合亚洲色一区二区三区| 亚洲Av无码国产情品久久| 久久天天躁狠狠躁夜夜躁2014|