• <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>
            面對(duì)現(xiàn)實(shí),超越自己
            逆水行舟,不進(jìn)則退
            posts - 269,comments - 32,trackbacks - 0

            C++內(nèi)存泄露的檢測(cè)(一)



            Visual Studio 調(diào)試器和 C 運(yùn)行時(shí) (CRT) 庫(kù)為我們提供了檢測(cè)和識(shí)別內(nèi)存泄漏的有效方法。主要使用函數(shù):_CrtDumpMemoryLeaks();

            二 實(shí)例
            #define _CRTDBG_MAP_ALLOC   //輸出更詳細(xì)的report
            #include 
            <stdlib.h>
            #include 
            <crtdbg.h>
            //以上的內(nèi)容必須放在其他include的前面

            #include 
            <vector>

            class MyClass
            {
            private:
                
            int *p;
            public:
                MyClass()
                
            {
                    
            if(p != NULL)
                    
            {
                        p 
            = new int(0);
                    }

                }

                
            ~MyClass()
                
            {
                    
            if(p != NULL)
                    
            {
                        delete p;
                        p 
            = NULL;
                    }

                }

            }
            ;

            int _tmain(int argc, _TCHAR* argv[])
            {
                
            int *= NULL; // better for read
                i = new int(0);    
                
            int *&= i; // pointer's reference

                MyClass 
            *pMyClass = new MyClass();

                std::vector
            <MyClass*> myClasses;
                myClasses.push_back(
            new MyClass());
                myClasses.push_back(
            new MyClass());

                _CrtDumpMemoryLeaks();
                
            return 0;
            }

            三說明
            1)只對(duì)debug模有用,可以在程序運(yùn)行后在vs的ide的output的最后看到泄露的檢測(cè)結(jié)果。
            2)可以檢測(cè)系統(tǒng)類型,自定義類型和stl 容器。
            3)#define _CRTDBG_MAP_ALLOC   //包含該宏定義輸出更詳細(xì)的report
                  #include <stdlib.h>
                  #include <crtdbg.h>
                  //以上的內(nèi)容必須放在其他include的前面,否則可能使上面定義的宏失效。
            4)如果程序有統(tǒng)一的退出口,則在退出時(shí)調(diào)用_CrtDumpMemoryLeaks();
            5)如果程序有多個(gè)出口,則可以在程序開始處包含下面的調(diào)用:_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );這條語(yǔ)句無論程序在什么地方退出都會(huì)自動(dòng)調(diào)用 _CrtDumpMemoryLeaks。

            四 更多(更多的API和demo的下載)
            http://msdn2.microsoft.com/zh-cn/library/fxszt639(VS.80).aspx

            五 其他同類文章
            http://www.shnenglu.com/zhouhuishine/archive/2008/01/22/41609.html


            C++內(nèi)存泄露的檢測(cè)(二)

            使用MFC提供的功能來檢測(cè)內(nèi)存泄露。

            使用方法:

            1)工程是MFC工程,或是工程的設(shè)置中有Use MFC in a Shared DLL,
            2)很多地方說是要定義以下宏
            #ifdef _DEBUG
            #define new DEBUG_NEW
            #endif
            但是我發(fā)現(xiàn)只要include <afx.h> 即可。(大家可以檢測(cè)一下)
            3)可以在F5運(yùn)行程序后,在output窗口中看到如下的內(nèi)存泄露的顯示。(只在debug下有用哦)
            4)如果有泄露,則顯示如下:
            Detected memory leaks!
            Dumping objects ->
            {214} normal block at 0x00D91618, 4 bytes long.
             Data: <    > 00 00 00 00
            {208} normal block at 0x00D914D0, 4 bytes long.
             Data: <    > 00 00 00 00
            {207} normal block at 0x00D91490, 4 bytes long.
             Data: <    > D0 14 D9 00
            {205} normal block at 0x00D91410, 4 bytes long.
             Data: <    > 00 00 00 00
            {204} normal block at 0x003AFFD8, 4 bytes long.
             Data: <    > 10 14 D9 00
            {203} normal block at 0x003AFF98, 4 bytes long.
             Data: <    > 00 00 00 00
            {202} normal block at 0x003AFF58, 4 bytes long.
             Data: <  : > 98 FF 3A 00
            {200} normal block at 0x003AFF18, 4 bytes long.
             Data: <    > 00 00 00 00
            Object dump complete.



            C++內(nèi)存泄露的檢測(cè)(三)



            一 簡(jiǎn)單的對(duì)內(nèi)存的分配和釋放跟蹤,并將結(jié)果輸出到console,它也是一般C++內(nèi)存泄露的檢測(cè)原理,來自C++編程思想:
            (比較簡(jiǎn)單,大家都可以看的明白的哦)實(shí)現(xiàn)如下:

            MemCheck.h
            //: C02:MemCheck.h
            #ifndef MEMCHECK_H
            #define MEMCHECK_H
            #include 
            <cstddef>  // for size_t

            // Hijack the new operator (both scalar and array versions)
            void* operator new(std::size_t, const char*long);
            void* operator new[](std::size_t, const char*long);
            #define new new (__FILE__, __LINE__)

            extern bool traceFlag;
            #define TRACE_ON() traceFlag = true
            #define TRACE_OFF() traceFlag = false

            extern bool activeFlag;
            #define MEM_ON() activeFlag = true
            #define MEM_OFF() activeFlag = false

            #endif
            ///:~

            MemCheck.cpp
            //: C02:MemCheck.cpp {O}
            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cassert>
            using namespace std;
            #undef new

            // Global flags set by macros in MemCheck.h
            bool traceFlag = true;
            bool activeFlag = false;

            namespace {

            // Memory map entry type
            struct Info {
              
            void* ptr;
              
            const char* file;
              
            long line;
            }
            ;

            // Memory map data
            const size_t MAXPTRS = 10000u;
            Info memMap[MAXPTRS];
            size_t nptrs 
            = 0;

            // Searches the map for an address
            int findPtr(void* p) 
            {
              
            for (int i = 0; i < nptrs; ++i)
                
            if (memMap[i].ptr == p)
                  
            return i;
              
            return -1;
            }


            void delPtr(void* p)
            {
              
            int pos = findPtr(p);
              assert(p 
            >= 0);
              
            // Remove pointer from map
              for (size_t i = pos; i < nptrs-1++i)
                memMap[i] 
            = memMap[i+1];
              
            --nptrs;
            }


            // Dummy type for static destructor
            struct Sentinel 
            {
              
            ~Sentinel()
              
            {
                
            if (nptrs > 0
                
            {
                  printf(
            "Leaked memory at:\n");
                  
            for (size_t i = 0; i < nptrs; ++i)
                    printf(
            "\t%p (file: %s, line %ld)\n",
                      memMap[i].ptr, memMap[i].file, memMap[i].line);
                }

                
            else
                  printf(
            "No user memory leaks!\n");
              }

            }
            ;

            // Static dummy object
            Sentinel s;

            }
             // End anonymous namespace

            // Overload scalar new
            void* operator new(size_t siz, const char* file,
              
            long line) {
              
            void* p = malloc(siz);
              
            if (activeFlag)
              
            {
                
            if (nptrs == MAXPTRS)
                
            {
                  printf(
            "memory map too small (increase MAXPTRS)\n");
                  exit(
            1);
                }

                memMap[nptrs].ptr 
            = p;
                memMap[nptrs].file 
            = file;
                memMap[nptrs].line 
            = line;
                
            ++nptrs;
              }

              
            if (traceFlag) 
              
            {
                printf(
            "Allocated %u bytes at address %p ", siz, p);
                printf(
            "(file: %s, line: %ld)\n", file, line);
              }

              
            return p;
            }


            // Overload array new
            void* operator new[](size_t siz, const char* file,
              
            long line) {
              
            return operator new(siz, file, line);
            }


            // Override scalar delete
            void operator delete(void* p) 
            {
              
            if (findPtr(p) >= 0
              
            {
                free(p);
                assert(nptrs 
            > 0);
                delPtr(p);
                
            if (traceFlag)
                  printf(
            "Deleted memory at address %p\n", p);
              }

              
            else if (!&& activeFlag)
                printf(
            "Attempt to delete unknown pointer: %p\n", p);
            }


            // Override array delete
            void operator delete[](void* p) {
              
            operator delete(p);
            }
             ///:~


            二 說明:
            1)通過重載new和delete來實(shí)現(xiàn)
            2)使用時(shí)需要在工程中加入MemCheck.h和MemCheck.cpp,在需要檢測(cè)的文件的前面include “MemCheck.h”,但是必須在所有的include的最后。
            3)MEM_ON(),MEM_OFF()用來打開或關(guān)閉檢測(cè)
            4)TRACE_ON(),和TRACE_OFF()用來打開或關(guān)閉檢測(cè)結(jié)果的輸出
            5)可以檢測(cè)代碼中使用了流,標(biāo)準(zhǔn)容器,以及某個(gè)類的構(gòu)造函數(shù)分配了空間

            三 使用實(shí)例:
            console 的project中加入下面的file:
            // MemoryLeak3.cpp : Defines the entry point for the console application.
            //

            #include 
            <iostream>
            #include 
            <vector>
            #include 
            <cstring>

            #include 
            "MemCheck.h"   // Must appear last!
            using namespace std;

            void Test()
            {
                
            int *= new int(0);
            }


            class MyClass
            {
            private:
                
            int *p;
            public:
                MyClass()
                
            {
                    
            if(p != NULL)
                    
            {
                        p 
            = new int(0);
                    }

                }

                
            ~MyClass()
                
            {
                    
            if(p != NULL)
                    
            {
                        delete p;
                        p 
            = NULL;
                    }

                }

            }
            ;

            void Test2()
            {
                
            int *= NULL; // better for read
                    i = new int(0);    
                    
            int *&= i; // pointer's reference
                    delete i;

                    MyClass 
            *pMyClass = new MyClass();

                    std::vector
            <MyClass*> myClasses;
                    myClasses.push_back(
            new MyClass());
                    myClasses.push_back(
            new MyClass());

                    std::vector
            <void*> myVector;
                    myVector.push_back(
            new MyClass());
                    myVector.push_back(
            new MyClass());
                    delete (MyClass 
            *)(myVector.at(0));
                    delete myVector.at(
            1); // memory leak
            }


            class Foo 
            {
              
            char* s;
            public:
              Foo(
            const char*s ) 
              
            {
                
            this->= new char[strlen(s) + 1];
                strcpy(
            this->s, s);
              }

              
            ~Foo() 
              
            {
                delete [] s;
              }

            }
            ;
            void Test3()
            {
                cout 
            << "hello\n";
                
            int* p = new int;
                delete p;
                
            int* q = new int[3];
                delete [] q;
                
            int* r;
                
            /*delete r;*/
                vector
            <int> v;
                v.push_back(
            1);
                Foo s(
            "goodbye");
            }

            int main() 
            {
                TRACE_OFF();
                MEM_ON();
                Test();
                Test2();
                Test3();
                MEM_OFF();
            }
             ///:~

            四 測(cè)試結(jié)果如下:


            C++內(nèi)存泄露的檢測(cè)(四)

            一 使用各種工具,一般都是收費(fèi)的,但是可以申請(qǐng)?jiān)囉谩?br />
            二 工具收集
            1)BoundsChecker :(http://www.compuware.com/)(首選BoundsChecker)
                  應(yīng)該說是功能最強(qiáng),使用只需要open需要測(cè)試的exe,然后start就可以了,可以通過檢測(cè)結(jié)果定位到源代碼中有內(nèi)存泄露的代碼行。
            2)Purifyplus (http://www.ibm.com
            3)Memory Validator(http://www.softwareverify.com/index.html
                   應(yīng)該說是功能也比較強(qiáng),使用只需要start application wizard的start exe就可以了,可以通過檢測(cè)結(jié)果定位到源代碼中有內(nèi)存泄露的代碼行。
            4)其實(shí)以上工具還可以進(jìn)行其他的各種檢測(cè),提高代碼的健壯性!


                工具只能幫助我們更好的發(fā)現(xiàn)泄露,但是并不能解決所有的問題,比如說我們的項(xiàng)目非常的復(fù)雜或是使用了多個(gè)第三方的lib,這樣的話,有可能使用以上的工具就檢測(cè)不到。
                 豐富的編程經(jīng)驗(yàn)和良好的編程習(xí)慣才能夠徹底的杜絕內(nèi)存的泄露。

            本文轉(zhuǎn)自:http://www.shnenglu.com/mzty/archive/2007/08/13/29922.html
            posted on 2012-10-24 13:17 王海光 閱讀(586) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++
            欧美久久综合九色综合| 久久国产精品-国产精品| 久久97久久97精品免视看秋霞| 国产亚洲欧美成人久久片| 性欧美丰满熟妇XXXX性久久久| 一本久久综合亚洲鲁鲁五月天| 久久久无码精品亚洲日韩软件| 91麻豆精品国产91久久久久久| 国内精品久久久久| 996久久国产精品线观看| 人妻少妇久久中文字幕| 少妇人妻88久久中文字幕| 久久久久亚洲精品无码蜜桃| 久久久久亚洲av无码专区| 久久久久成人精品无码中文字幕 | 国产亚州精品女人久久久久久| 99精品久久精品一区二区| 国产精品久久久久久福利漫画 | 久久精品国产一区二区| 久久精品国产精品亚洲| 亚洲第一永久AV网站久久精品男人的天堂AV | 久久久久四虎国产精品| 欧美久久综合性欧美| 国产精品免费久久| 日韩va亚洲va欧美va久久| 一97日本道伊人久久综合影院| 久久人人爽人人爽人人av东京热 | 午夜精品久久久内射近拍高清| 无码任你躁久久久久久| 一本一本久久aa综合精品| 久久国产精品成人片免费| 久久精品国产91久久麻豆自制 | 偷偷做久久久久网站| 久久精品国产精品亚洲精品| 久久av无码专区亚洲av桃花岛| 久久精品嫩草影院| 久久免费国产精品| 色婷婷综合久久久久中文| 欧美综合天天夜夜久久| 三级片免费观看久久| 久久精品国产亚洲AV高清热|