• <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的博客
            久久综合丁香激情久久| 欧美亚洲国产精品久久| 久久国产乱子伦精品免费强| 91麻精品国产91久久久久| 久久丝袜精品中文字幕| 奇米综合四色77777久久| 国产精品VIDEOSSEX久久发布| 国产精品成人久久久| 久久精品国产亚洲网站| 亚洲国产欧美国产综合久久| 久久99精品国产麻豆不卡| 久久精品国产第一区二区三区 | 国产一区二区精品久久岳| 久久人人添人人爽添人人片牛牛 | 一本大道加勒比久久综合| 久久夜色精品国产欧美乱| 亚洲精品美女久久久久99小说| 91精品国产91久久综合| 久久久无码精品亚洲日韩京东传媒| 国产成人精品久久综合| 狠狠色丁香婷婷久久综合不卡 | 国产精品久久久久久一区二区三区| 色偷偷88欧美精品久久久| 久久国产美女免费观看精品 | 99久久中文字幕| 久久丫精品国产亚洲av不卡 | 狠狠综合久久综合中文88| 久久99精品综合国产首页| 久久99精品久久久久婷婷| 亚洲国产一成人久久精品| 久久亚洲sm情趣捆绑调教| 色婷婷久久综合中文久久一本| 欧美激情精品久久久久久| 久久亚洲AV永久无码精品| 亚洲国产精品无码久久久久久曰| 国产精品九九久久免费视频 | 久久精品国产网红主播| 999久久久无码国产精品| 久久se精品一区精品二区| 伊人久久大香线焦综合四虎| 国产精品gz久久久|