• <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的博客
            久久久久国色AV免费观看| 精品久久人人爽天天玩人人妻| 99久久精品国产免看国产一区| 久久婷婷五月综合97色| 亚洲国产精品久久66| 久久婷婷五月综合色奶水99啪| 久久久免费精品re6| 久久久久国产一级毛片高清板 | 91久久精品视频| 午夜视频久久久久一区 | 亚洲一级Av无码毛片久久精品| 色综合久久久久综合体桃花网| 久久久久国产精品嫩草影院 | 久久精品成人| 精品久久久无码人妻中文字幕豆芽| 精品久久久无码中文字幕| 亚洲精品乱码久久久久66| 久久久久亚洲?V成人无码| 国产精品一区二区久久不卡| 久久婷婷国产剧情内射白浆| 久久精品亚洲乱码伦伦中文| 精品久久人妻av中文字幕| 无码久久精品国产亚洲Av影片| 久久久久亚洲精品无码网址| 91久久成人免费| 91精品国产高清久久久久久91| 久久久老熟女一区二区三区| 国产成人精品综合久久久 | 91精品国产综合久久久久久| 三级三级久久三级久久| 亚洲国产精品综合久久一线| 中文精品99久久国产| 亚洲国产成人久久一区WWW| 久久亚洲2019中文字幕| 伊人精品久久久久7777| 热久久国产欧美一区二区精品| 精品久久久久久国产三级| 国产精品久久久99| 国内精品久久久久久久影视麻豆| 狠狠精品干练久久久无码中文字幕 | 日本久久久精品中文字幕|