VC-CRT debugging

Source: Internet
Author: User

Programmers who develop based on VC tools are certainly familiar with the concept of C Runtime Library (CRT, most C/C ++ programs usually need static or dynamic link CRT. The Compiler generally uses CRT as the default Library Link and does not need explicit settings, most people may only use the CRT function to call some tool functions, such as string, date and time, file, input and output, and operating system interface packaging, however, the powerful debugging function of CRT is ignored. This article will briefly introduce the important debugging support provided by CRT. For details, refer to msdn. Note that the CRT debugging function is only available in the debugging version, including the crtdbg. h header file. The project must have at least two settings:

1. Pre-processing macro _ debug is defined;

2. debug version of dynamic or static link CRT

Most of the debugging functions of CRT are written around the debugging heap, that is, dynamic memory debugging and diagnosis. Other tools include macro and debugging reporting mechanisms and application hook mechanisms. The following is a detailed analysis.

 

1. debug the report mechanism

This is the way that CRT provides debugging information to users. An asserted, error dialog box, and warning messages from the debugging window during running are usually displayed in the debugging program. These are all debugging reports issued by CRT, many other debugging functions use this basic service, including memory dump presentation, Memory Leak reports, user-used assert macros such as _ assert and _ asserte, which call the debugging report service.

The debugging report is divided into three types based on the nature of the report content: warn, assert, error. The report has three forms: Debugging window, pop-up dialog box, file (including standard output ), by default, the warn type report is output to the debugging window. The other two are displayed in the dialog box. Use the following three functions to provide corresponding services:

1). _ crtsetreportmode: sets the output mode of each type.

2). _ crtsetreportfile: sets the output file.

3). _ crtdbgreport: outputs the debugging report.

To facilitate the use of the debugging report service, CRT defines many macros, including _ assert, _ asserte, and rptx. The most common macros are _ assert and _ asserte asserted macros, you can use rptx to output debugging reports of other types. These macros call _ crtdbgreport (,). You can call these macros to debug programs where appropriate.

Some users may not be satisfied with the preceding three report Presentation Methods. If you want to customize your report output mode or track the output of the report, you can use _ crtsetreporthook (...) set your own report hook function. In this way, when the CRT needs to output the debugging report, it first calls the hook function set by the user. If the return value of the hook function is true, the CRT will not perform the default processing, otherwise, proceed to the normal processing process.

 

2. debug the heap Check Mechanism

This should be the core debugging function of CRT. The memory allocated by malloc/new in the debugging version program comes from the debugging heap. The most bugs in the C/C ++ program are related to the memory, in particular, dynamic memory, including memory leakage, heap overflow, heap corruption, and free memory re-use. The debugging heap check mechanism provides many functions to detect memory problems during runtime. To this end, CRT implements its own debugging heap manager to support the implementation of corresponding functions.

The debugging heap manager allocates an additional Block Structure and heap overflow detection interval memory for each user's memory. Therefore, the debugging version allocates more memory, the block header structure records the corresponding information of the memory (size, type, file to be allocated and row number). All the block headers form a two-way linked list, then save the latest linked list header pointer. Each time the user's memory is allocated successfully, the debugging heap manager sets all heap overflow detection interval memory to 0xfd, so that you can check whether the memory is 0xfd as appropriate to determine whether a heap overflow occurs.

With the debug heap manager, you can easily provide various memory debugging functions. The CRT provides services through the following functions:

1 ). _ crtsetdbgflag: You can set different switches to change the corresponding behavior of the CRT. For example, whether to check the integrity of the heap and whether to report the memory leakage before the program exits.

2). _ crtmemcheckpoint: obtains the allocation status of the current heap.

3). _ crtmemdifference: Check the heap status changes at different times.

4). _ crtcheckmemory: checks the integrity of the heap.

5). _ crtdumpmemoryleaks: Reports Memory leakage.

6). _ crtmemdumpallobjectssince: reports heap memory information at a time point.

7). _ crtsetallochook: sets the allocation hook to track each heap allocation or release.

8). _ crtsetbreakalloc: sets the allocation breakpoint to track a allocation request. After the allocation request appears during the debugging process, the program will be interrupted.

 

 

 

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.