VC ++ compilation exception description

Source: Internet
Author: User

To learn VC well, you must have a good C/C ++ Foundation, and the necessary English reading ability is also essential, because a large number of technical documents are published in English, otherwise, the VC ++ compilation exception will occur, which greatly affects the programmer's efficiency.

Recall the EXCEPTION_REGISTRATION structure I introduced in section 1. We used it to register the callback function to be called in case of an exception with the operating system. VC ++ does the same, but it extends the semantics of this structure and adds two new fields after it:

 
 
  1. struct EXCEPTION_REGISTRATION   
  2. {   
  3.     EXCEPTION_REGISTRATION* prev;   
  4.     DWORD handler;   
  5.     int id;   
  6.     DWORD ebp;   
  7. };  

When a VC ++ compilation exception occurs, a local variable of the prediction_registration type is added to the vast majority of functions. The last field ebp of the variable overlaps with the position pointed to by the stack callback pointer. The preface to the letter number creates this structure and registers it to the operating system. At the end, the prediction_registration of the main function is restored. I will introduce the meaning of the id field in the next section.

When compiling a function, VC ++ generates two data parts for it:
A) exception callback function
B) a data structure that contains important information about the function. The information includes catch blocks, addresses of these blocks, and types of exceptions that these blocks are concerned. I call this structure funcinfo, and the detailed discussion about it is also in the next section.

The runtime stack after exception handling is considered. The exception callback function of the widget is located at the beginning of the exception handling chain pointed to by FS: [0], which is set by the preface of the widget ).

The exception handler submits the address of the widget's funcinfo structure to the function _ CxxFrameHandler, __cxxframehandler checks the structure to see if any catch Block in the function is interested in the current exception.

If not, it returns predictioncontinuesearch to the operating system. Therefore, the operating system retrieves the next node from the exception handling linked list, and the exception handler that calls it, that is, the exception handler that calls the current function ).

This process will continue until the handler finds a catch Block that can handle the current exception, and then it will no longer return to the operating system. However, because the funcinfo structure exists before the catch Block is called, the catch block entry is known. See figure 3). The stack must be expanded, that is, all other stack shards under the current function stack shards are cleared. This operation is a little complicated.

Because: The exception handler must find all local objects that exist on the stacks when exceptions occur. VC ++ compiles exceptions and calls their destructor in turn. I will introduce this in detail later. The exception handler delegates this work to the exception handling programs of each stack worker. Starting from the first node of the exception handling chain pointed to by FS: [0], it calls the processing program of each node in sequence and tells it that the heap stack is expanding.

In response, these handlers call the destructor of each local object and return the result. This process continues until the node corresponding to the exception handling program itself. A catch Block is a part of a function, so it uses the function stack iterator. Therefore, before calling a catch Block, the exception handler must activate the stack handler of the function to which it belongs.

Second, each catch block only accepts one parameter. The type of the VC ++ compilation exception is the type of the exception it wants to capture. The exception handler must copy the reference of the exception object itself or the exception object to the stack iterator Of The catch Block. the compiler records the relevant information in funcinfo, based on this information, the handler can know where to copy the exception object.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.