• <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 常興龍 閱讀(199) 評論(0)  編輯 收藏 引用
            > hi的博客
            国内精品久久久久影院薰衣草 | 亚洲性久久久影院| 久久久久久A亚洲欧洲AV冫| 亚洲精品国产综合久久一线| 久久久久久久免费视频| 国内精品伊人久久久久AV影院| 91精品国产高清久久久久久国产嫩草| 久久久WWW成人免费毛片| 亚洲国产成人久久笫一页| 新狼窝色AV性久久久久久| 国产无套内射久久久国产| 色妞色综合久久夜夜| 午夜不卡888久久| 亚洲AV无码成人网站久久精品大| 岛国搬运www久久| 久久国产精品99精品国产| 曰曰摸天天摸人人看久久久| 亚洲人成伊人成综合网久久久| 99久久无码一区人妻| 久久九九精品99国产精品| 久久国产视频网| 7777久久亚洲中文字幕| 亚洲国产精品成人久久| 日本欧美国产精品第一页久久| 久久精品国产99久久久| 91麻豆国产精品91久久久| 久久播电影网| 久久免费国产精品| 精品国产综合区久久久久久 | 国产美女久久精品香蕉69| 久久中文字幕人妻熟av女| 国产精品狼人久久久久影院| 91精品国产色综合久久| 久久久久亚洲av无码专区导航| 久久人妻少妇嫩草AV蜜桃| 久久久久无码精品| 久久久久亚洲av成人无码电影 | 国产呻吟久久久久久久92| 91久久精品视频| 久久黄视频| 香蕉久久夜色精品国产尤物|