Memory leakage detection

Source: Internet
Author: User

. 1. Memory leakage check in VS 2008

 

The main tool used to detect memory leaks is the debugger and CRT to debug heap functions. To enable the debugging heap function, include the following statements in the program:

 

# Define _ CRTDBG_MAP_ALLOC

# Include <stdlib. h>

# Include <crtdbg. h>

# Ifdef _ DEBUG

# Define new (_ NORMAL_BLOCK ,__ FILE __,__ LINE __)

# Endif

 

 

A) by including crtdbg. h, the malloc and free functions are mapped to their "Debug" version _ malloc_dbg and _ free_dbg. These functions track memory allocation and release. This ing only occurs in the DEBUG version (where _ DEBUG is defined. The released version uses common malloc and free functions.

B) # The define _ CRTDBG_MAP_ALLOC statement maps the base version of the CRT heap function to the corresponding Debug version. This statement is not absolutely required, but without it, the memory leak dump contains less useful information.

C) after the preceding statements are added, You can include the following statements in the program to dump Memory leakage information:

_ CrtDumpMemoryLeaks ();

When running a program in the debugger, _ CrtDumpMemoryLeaks will display Memory leakage information in the "output" window. The memory leakage information is as follows:

 

Detected memory leaks!

Dumpingobjects->

X: XX. cpp (172): {1875} normal block at 0x00780E80, 78600 bytes long. // Memory Allocation Number, block type (common, client, or CRT), memory location, block size

Data: <> CD // the first 16 bytes of content (hexadecimal)

Object dumpcomplete.

 

---- If the program can exit from multiple locations, the following calls are included at the beginning of the program:

 

_ CrtSetDbgFlag (_ CRTDBG_ALLOC_MEM_DF | _ CRTDBG_LEAK_CHECK_DF );

This statement automatically calls _ CrtDumpMemoryLeaks when the program exits. The _ CRTDBG_ALLOC_MEM_DF and _ CRTDBG_LEAK_CHECK_DF fields must be set at the same time, as shown in the preceding figure.

---- Determine the memory leak location

 

Add the following line (allocated for 1875th memories ):

_ CrtBreakAlloc = 1875; // when the memory allocation is applied for 1875th times

Alternatively, you can use the _ CrtSetBreakAlloc function with the same effect:

_ CrtSetBreakAlloc (1875 );

When the breakpoint stops, open the "call stack" window and find the corresponding source code;

Exit the program, observe the Memory Leak report in the "Output Window", and check whether the block number actually allocated in the memory is the same as the preset value. If the block number is the same, it is found. If the block number is different, repeat Step 3 until the same.

 

(Source: http://blog.sina.com.cn/s/blog_4bf793ad0100jsxy.html)

 

 

2. Memory leakage solution

Smartpointer ---- STL has been widely used

Garbadgecollection ---- java

 

3. Memory leakage generally refers to heap memory leakage.

Apply for heap memory in Malloc, realloc, and new modes

Release Methods: free and delete

 

4. resource leak)

Core HANDLE, GDIObject, SOCKET, Interface, etc. Basically, these objects allocated by the operating system also consume memory. If these objects are leaked, memory leakage will eventually occur.

 

5. to detect heap memory leakage, You need to intercept malloc/realloc/free and new/delete (in fact, new/delete will eventually use malloc/free, so you only need to intercept the previous group ). For other leaks, you can use a similar method to intercept the corresponding allocation and release functions. For example, to detect BSTR leaks, you must intercept
SysAllocString/SysFreeString; to detect HMENU leaks, you must intercept CreateMenu/DestroyMenu

 

6. windows Memory leakage detection tools:

MSC-Runtime Library built-in detection function;

Third-party (external mounting) detection tools: Purify, boundschecker;

Embedded cemonitor in Windows

7. BoundsChecker

A Code Injection technique is used to intercept calls to functions that allocate and release memory. To put it simply, when your program starts running, the BoundsChecker DLL is automatically loaded into the address space of the process (which can be implemented through the system-level Hook ), then it modifies the function calls for memory allocation and release in the process, so that these calls are first transferred to its code, and then the original code is executed. BoundsChecker does not need to modify the source code or project configuration file of the program to be debugged, which makes it very simple and direct.

 

8. The basic destructor should be declared as virtual.

Raià: Apply for resources in the constructor and release resources in the destructor. Example: auto_ptr in STL

 

9. CodeWizard

More than 500 built-in encoding rules can be combined at will

Seamless integration with vs c ++

 

10. Code check tool PC-Lint
PC-Lint can check for errors that are not easily discovered by the compiler. PC-Lint can check more than 100 C-library functions and discover more than common errors in Standard C/C ++ code.

 

11. Use Purify and Insure ++ to find runtime memory Defects
Rational Purify and Parasoft Insure ++ are tools used for runtime error checks. Purify mainly checks: Array Memory out-of-bounds read/write, use uninitialized memory, read/write the released memory, and memory leakage. Insure ++ 12.
With its patented technology (source code plug-in and runtime pointer tracking), it can detect a large number of Memory Operation errors, report the wrong source code lines and execution tracks. According to the author's test (based on 98 C ++ programs with various memory errors, covering typical cases), Insure ++ 6.1 can be accurately detected.

 

Use Windows structured exception handling mechanism to handle memory crashes of released software versions
During the program release stage, program errors, especially memory crashes, should be minimized. If the program crashes, you should exit elegantly and try to collect running information when the program crashes to help the program supplier with subsequent debugging. To capture illegal memory access and obtain the instruction address and register content for illegal access, you need to use the Windows Structured Exception Handling (SEH) mechanism [6]. MiniDumpWriteDump is dbghelp. an API function provided by dll (refer to MSDN), used to dump some information of user mode programs (such as stack conditions) and co-exist as a file (such. dmp file). This file can be used by Microsoft debugger.
(VC ++ or WinDBG. Using this function requires dbghelp. h, dbghelp. lib, and dbghelp. dll (these files can be found in the Windows Platform SDK ).
To debug the code based on the. dmp file afterwards, you need to generate the DEBUG symbols (pdb) file for the released software version (enable the compiler/debug option ). After obtaining the. dmp file, use VC ++ to open the. dmp file and debug the file (Press F5 ). In this way, the crash scene will reappear. Document [5] implements the crash reporting system based on the above method

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.