• <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++細(xì)節(jié)深度探索及軟件工程

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              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 常興龍 閱讀(197) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            > hi的博客
            无码日韩人妻精品久久蜜桃| 国产免费久久精品99久久| 精品国产99久久久久久麻豆| 久久久久人妻一区二区三区 | 曰曰摸天天摸人人看久久久| 国产精品久久永久免费| 久久国产视频网| 久久久久亚洲AV片无码下载蜜桃| 国产91久久精品一区二区| 成人精品一区二区久久久| 久久久久女教师免费一区| 亚洲欧美伊人久久综合一区二区| 99999久久久久久亚洲| 欧美一级久久久久久久大| 久久久久99精品成人片直播| 精品久久国产一区二区三区香蕉| 久久天天躁夜夜躁狠狠| 亚洲国产精品久久久久婷婷老年| yy6080久久| 久久久中文字幕日本| 国产99精品久久| 久久SE精品一区二区| 久久久久亚洲av毛片大| 久久精品国产亚洲一区二区| AV无码久久久久不卡蜜桃| 久久久久亚洲AV成人网| 久久精品国产精品青草| 久久夜色精品国产噜噜亚洲AV| 亚洲国产天堂久久久久久| 超级碰久久免费公开视频| 日韩精品久久久久久| 精品熟女少妇av免费久久| 久久永久免费人妻精品下载| 婷婷综合久久中文字幕蜜桃三电影 | 99久久精品久久久久久清纯| 精品乱码久久久久久久| 久久精品欧美日韩精品| A狠狠久久蜜臀婷色中文网| 色综合久久久久综合体桃花网| 久久人人爽人人爽人人片AV东京热 | jizzjizz国产精品久久|