Windows平台上C++開發記憶體流失檢查方法

來源:互聯網
上載者:User

Windows平台上C++開發記憶體流失檢查方法 充分的利用調試工具可以非常方便地避免記憶體流失問題。 這裡介紹兩種方法,互為補充,第一種是VC編譯器提供的方法,第二種是專用的記憶體流失檢查工具Memmory Validator。這兩種方法的基本原理是一樣的:記憶體配置要通過CRT在運行時實現,只要在分配記憶體和釋放記憶體時分別做好記錄,程式結束時對比分配記憶體和釋放記憶體的記錄就可以確定是不是有記憶體流失。其中,第一種方法重載了new操作符,第二種方法是替換了CRT執行階段程式庫,在使用者程式與運行庫之間加了一層,用於記錄記憶體配置情況。兩種方法的不同是前者是在編譯時間完成的,分析記憶體情況的代碼編譯到執行檔案中,用於程式的debug版本,後一種對編譯過程沒有影響,在執行過程中截留與CRT的互動資訊。 第一種方法是MSDN中介紹了,在需要檢查記憶體配置情況的cpp檔案中引用和兩個標頭檔,放於最頭部,再用宏代換的方法用重載後的new操作符代替原來的new操作符,如下列代碼所示,在程式退出的位置調用_CrtDumpMemoryLeaks()輸出全部沒有釋放的記憶體內容,及申請這些記憶體的原始碼位置,非常便於調試。 但這種方法有缺陷,_CrtDumpMemoryLeaks()必須在main()函數結束之前調用,所以main()函數中的棧對象還沒有析構,會被當做記憶體流失,如下面代碼中的NewClass類的對象,實際是沒有問題的。另外就是不同編譯器實現非ASCII碼字元時可能會有所不同,如下面代碼中的漢字串都被視為記憶體流失。 #include #include #include #include #include using namespace std; #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) #define new DEBUG_NEW struct AlertDescriptionString { unsigned int ID; string Name; string Desp; }; const AlertDescriptionString String[] = { {0,"AAA","BBBB"}, // memory leak count by _CRTDBG_MAP_ALLOC {1,"CCC","__XXX__檢測到可能由AirJack發出的資料包"} }; struct AlertDescriptionStr { unsigned int ID; char* Name; char* Desp; }; const AlertDescriptionStr Str[] = { {0,"AAA","BBBB"}, {1,"CCC","檢測到可能由AirJack發出的資料包"} }; void leak_memory(void) { vector vec; for( int j = 0; j < 2; j++) { vec.push_back(new unsigned int(0)); } } class NewClass { void * p ; public: NewClass() { p = (void*) new char[10]; } ~NewClass() { delete [] p; } }; int * p = new int(9); int real_main_fun (int argc, char ** argv) { leak_memory(); return 0; } int main (int argc, char ** argv) { string str("__YYY__檢測到可能由AirJack發出的資料包"); NewClass cls; int i = real_main_fun(argc, argv); // 原有的main函數體 if(_CrtDumpMemoryLeaks()) cout<< " memory leak " << endl; return i; } 第二種方法正好是它的補充,Memmory Validator可以檢查程式整個運行過程中的記憶體配置情況,也可以將記憶體流失的位置顯示出來,如圖所示。 在實際應用第一種方法時,可以採用兩個標頭檔,用於大型工程的調試,幾乎不對其他部分代碼產生影響。 ----------------------------------------------------------------------------- //config.h #define MEMORY_DEBUG #ifdef MEMORY_DEBUG #include #include #endif // MEMORY_DEBUG ------------------------------------------------------------------------------ //debug.h #ifdef MEMORY_DEBUG #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) #define new DEBUG_NEW #endif // MEMORY_DEBUG ------------------------------------------------------------------------------- //main.cpp #include "config.h” // config.h, 第一個標頭檔 #include "alertparser.h" #include "alertinfocache.h" #include #include #include "debug.h" //debug.h, 最後一個標頭檔 int main (int argc, char ** argv) { int i = real_main_fun(argc, argv); // 原有的main函數體 if(_CrtDumpMemoryLeaks()) cout<< " memory leak " << endl; return i; }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.