Reading Note _ initialization and cleanup of the running database

Source: Internet
Author: User

 

Initialization and cleanup of the Runtime Library

The compiler will automatically insert an "compiled by the compiler" entry function for each module. In this entry function, initialize the function and call the user's entry function, after the user's entry function returns, we are running our own cleaning function. We call this entry function inserted by the compiler the CRT entry function.

Entry functions of the EXE Module

User entry function

CRT entry function

Application

Main

MainCRTStartup

Console Program

Wmain

WmainCRTStartup

Wide character console Program

WinMain

WinMainCRTStartup

Win32 Application

WWinMain

WWinMainCRTStartup

Wide-character Win32 Application

The user portal function of the DLL module is dllmain. Because dllmain does not have a character type parameter, the portal function of the DLL module of the wide character (UNICODE) is also dllmain.

The CRT entry function prepared by the compiler for the DLL module is the _ dllmaincrtstartup function. Its function prototype is identical to that of dllmain.

In the connection phase, the module entry function is registered to the target program file (PE file). By default, the linker registers the CRT entry function as the module entry function, in this way, the CRT entry function is first executed during the execution process, and the CRT entry function is initialized. After the user's entry function returns, the CRT entry function can be cleaned up.

In addition, the linker supports specifying other functions as the entry through the/entry: function option. If you specify entry-point symbol as the winmain function name in the Project LINK attribute (output), or directly add/entry: "winmain" to the command line parameters ", set the winmain function as the program entry. In this case, you must consider how to initialize and clean up the runtime.

The following describes how to use the Runtime Library for multiple modules in a process. For static links to run the library program module, both EXE and DLL, each module copies a copy of The Runtime Library variable and code, so that each module has an instance of the Runtime Library. When there are multiple runtime database instances in the process, each runtime database instance uses its own data (variables) and resources (HEAP), which can work properly theoretically. But it may also cause problems. One of them is that the memory allocated from one CRT heap is sent to another CRT instance for release, in the debugging version, the CRT memory check function will detect this problem and report an error. However, in a released version, this will cause serious problems.

Run-time errors is a variety of checks performed during the runtime, mainly to find the various errors exposed during the runtime. The compiler usually takes the following measures:

1. Use the support library and library functions of the debug version. The library functions of the debug version include more debugging support and check functions. For example, the memory allocation and release function of the debug version inserts additional information to support various memory check functions.

2. insert additional code during compilation to check the stack pointer and local variables.

3. Provides assert, report (_ RPT), and other mechanisms for programmers to add checking code and reporting runtime errors when writing programs.

The first two are compiler behaviors, and the third is manual insertion.

The following uses the vc8 compiler as an example to check the runtime errors that can be found during the automatic runtime check:

1. Stack pointer uption. The function used to check the stack pointer is _ rtc_checkesp.

2. The local variables allocated to the stack are out of bounds (overruns), and the resulting stack corruption uption)

3. dependent on uninitialized local variables, the RTC function responsible for this function is _ rtc_uninituse. If no initialized local variables are used in the program, then the compiler will give the c4700 or c4701 warning during compilation, but these two warnings belong to level 4 warning, so they may be blocked or ignored. If the/rtcu switch is enabled, an error report dialog box is displayed when the program runs to use uninitialized local variables.

4. Data loss is caused by assigning values to shorter variables. The RTC function responsible for this check is _ rtc_check_x_to_y, where X and Y can be a combination of values 8, 4, 2, and 1.

5. Heap-related errors.

For assertions, the assertions of the VC compiler are _ assert and _ asserte. The MFC framework defines the assert macro and verify macro, and the Standard C also provides an Assert (all lowercase) Macro, the usage is basically the same.

 

When an error is found, the _ CrtDbgReport function is used to report the error. Both the asserted failure and the _ RPT macro report the debugging information by calling the _ CrtDbgReport function.

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.