Simple memory debugging technology

Source: Internet
Author: User

I
Check Memory leakage


Add the following statement:

# Define crtdbg_map_alloc

# Include <stdlib. h>

# Include <crtdbg. h>

The first line of the above Code is to output some additional information, such as the file name and number of lines where the code for the leaked memory block is located. The second and third lines map the malloc and free functions to the "debug" version _ malloc_dbg and _ free_dbg to track memory allocation and release. This ing only occurs in the debugging version. The release version uses common malloc and free functions.



Called when the program exits
_ Crtdumpmemoryleaks ()
Output debugging information to the debugging window of VC

,
You can also set a flag to automatically output debugging information when the program exits, as shown below:

_ Crtsetdbgflag (_ crtdbg_report_flag) | _ crtdbg_leak_check_df)



Sometimes the program leaks information without the file name and number of lines, for example:


Detected memory leaks!


Dumping objects->


{52} normal block at 0x06ac2c88, 1234 bytes long.

 

Data: <
> CD


Object dump complete.

Above {52}
Indicates the number of the memory block, which is fixed in most cases. You can pass this number
_ Crtsetbreakalloc to create a breakpoint. Execution will be interrupted just before the block is allocated. You can trace up to determine which routine has performed an incorrect call. Then exit the program and observe "fail ".
Out-of-the-window "Memory leakage information, verify that the actual memory allocation number is the same as the default value, if not, it indicates that the breakpoint is invalid, you need to try another number to trace again.






 

II
Memory Operation out-of-bounds check



Sometimes memory operations are out of bounds. You can use the _ crtcheckmemory function to verify the validity of all allocated memory blocks (for example, whether the boundary on both sides of the memory block is rewritten). You can insert the following code into all suspicious points, locate the code location where memory out of bounds occurs:
Assert (_ crtcheckmemory ());

You can also use the _ crtsetdbgflag function to set the _ crtdbg_check_always_df flag. In this way, the system automatically calls _ crtcheckmemory each time the memory is allocated/released, helping to quickly capture memory errors.



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.