C ++ checks for Memory leakage

Source: Internet
Author: User

Note: The ide I use is vs2008 1. the project is set to debug Memory leakage detection generally in debug mode 2. add [cpp] # ifdef _ DEBUG # define _ CRTDBG_MAP_ALLOC # include <stdlib. h> # include <crtdbg. h> # define new (_ NORMAL_BLOCK, _ FILE __, _ LINE _) # endif 3. insert such a sentence into the code: EnableMemLeakCheck (); [cpp] inline void EnableMemLeakCheck () {_ CrtSetDbgFlag (_ CRTDBG_REPORT_FLAG) | _ timeout);} 4. then we can see the leakage in the output. For example, I use the newEx table. New [cpp] int _ tmain (int argc, _ TCHAR * argv []) {EnableMemLeakCheck (); int num = 10; byte ** p = newEx byte * [num]; for (int I = 0; I <num; I ++) {Sleep (1 ); * p = newEx byte [I];} long * pl = newEx long [100]; while (1) {Sleep (100);} return 0 ;} the content displayed in the output (run the program in debug, and then close the program with a cross-fork) memory leaks! Dumping objects-> e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (101): {124} normal block at 0x00295CB8, 400 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92 ): {122} normal block at 0x00294C30, 9 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_me M_leak.cpp (92): {121} normal block at 0x00294BE8, 8 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): {120} normal block at 0x00299F88, 7 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): {119} normal block at 0x00299F40, 6 bytes long. data: <> CD e: \ project \ Test \ test_mem_leak \ test_mem_leak.cpp (92): {118} normal block at 0x00299EF8, 5 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): {117} normal block at 0x00299EB8, 4 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): {116} normal block at 0x00299E78, 3 bytes long. data: <> CD CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92) :{ 115} normal block at 0x00299E38, 2 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): {114} normal block at 0x00299DF8, 1 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): {113} normal block at 0x00299DB8, 0 bytes long. data: <> p e: \ proj Ect \ test \ test_mem_leak \ test_mem_leak.cpp (88): {112} normal block at 0x00299D50, 40 bytes long. data: <0L)> 30 4C 29 00 CD Object dump complete. the file name and row number are available. Apply for the file first and then release it. Extension: another new is used in dbgnew. [cpp] void * _ CRTDECL operator new [] (size_t cb, int nBlockUse, const char * szFileName, int nLine) _ THROW1 (_ STD bad_alloc) {void * res = operator new (cb, nBlockUse, szFileName, nLine); RTCCALLBACK (_ RTC_Allocate_hook, (res, cb, 0 )); return res;} note that the const char * szFileName can use _ file _ or a custom class, which is overloaded as char * (thanks to the old horse for providing Code). For example: [cpp] char new_index_recorder_file_name [1 024*1024] [256]; class new_index_recorder {public: new_index_recorder (char * file, int line): file (file), line (line) {} operator char *() {static unsigned int index; sprintf (new_index_recorder_file_name [index], "% s (% d): % d", file, line, index ++ ); return new_index_recorder_file_name [index];} private: char * file; int line ;}; Of course, you can also use a function to return the char * pointer [cpp] int g_count = 0; class OperNew {public: OperN Ew () {g_count ++; pC = new char [20]; memset (pC, 0, 10); sprintf (pC, "No. % d ", g_count);} char * GetChar () {return pC;} private: char * pC;}; then you can return custom content. In this return, a sequence number is added during the construction, and the time can also be added. Results: e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (95): 11 (95): {123} normal block at 0x002B4C78, 400 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_leak \ test_mem_leak.cpp (92): 10 (92) :{ 122} normal block at 0x002B4C30, 9 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ tes T_mem_leak.cpp (92): 9 (92): {121} normal block at 0x002B4BE8, 8 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): 8 (92 ): {120} normal block at 0x002B9F88, 7 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): 7 (92): {119} normal block at 0x002B9F40, 6 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): 6 (92): {118} normal block at 0x002B9EF8, 5 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): 5 (92): {117} normal block at 0x002B9EB8, 4 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): 4 (92) :{ 116} Normal block at 0x002B9E78, 3 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): 3 (92): {115} normal block at 0x002B9E38, 2 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (92): 2 (92): {114} normal block at 0x002B9DF8, 1 bytes long. data: <> CD e: \ project \ test \ test_mem_leak \ test_mem_leak.c Pp (92): 1 (92): {113} normal block at 0x002B9DB8, 0 bytes long. data: <> h e: \ project \ test \ test_mem_leak \ test_mem_leak.cpp (88): 0 (88): {112} normal block at 0x002B9D50, 40 bytes long. data: <0L +> 30 4C 2B 00 CD Object dump complete. the program "[8796] test_MEM_LEAK.exe: Local Computer" has exited and the returned value is-1073741510 (0xc000013a ). Tip: copy the output file to the UE, and then find the number of times the leaked row number appears. You can calculate the number of leaks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.