Application termination

The main function is the entry point for your application. However, this isn't called directly by the operating system because C++ will perform initialization before main is called. This includes constructing the Standard Library global objects (cin, cout, cerr, clog, and the wide character versions) and there is a whole host of initialization that is performed for the C Runtime Library that underpins C++ libraries. Further, there are the global and static objects that your code creates. When the main function returns, the destructors of global and static objects will have to be called and a clean-up performed on the C runtime.

There are several ways to stop a process deliberately. The simplest is to return from the main function, but this assumes that there is a simple route back to the main function from the point that your code wants to finish the process. Of course, process termination must be ordered and you should avoid writing code where it is normal to stop the process anywhere in the code. However, if you have a situation where data is corrupted and unrecoverable and any other action could damage more data, you may have no option other than to terminate the application.

The <cstdlib> header file provides access to the header files to the functions that allow you to terminate and to handle the termination of an application. When a C++ program closes down normally, the C++ infrastructure will call the destructors of the objects created in the main function (in the reverse order to their construction) and the destructors of static objects (which may have been created in functions other than the main function). The atexit function allows you to register functions (that have no parameters and no return value) that will be called after the main function completes and static object destructors have been called. You can register more than one function by calling this function several times, and at termination the functions will be called in reverse order to their registering. After the functions registered with the atexit function have been called, the destructors of any global objects will be called.

There is also a Microsoft function called _onexit that also allows you to register functions to be called during normal termination.

The exit and _exit functions perform a normal exit of a process, that is, they clean up the C runtime and flush any open files before shutting down the process. The exit function does additional work by calling any registered termination functions; the _exit function does not call these termination functions and so is a quick exit. These functions will not call the destructors of temporary or automatic objects, so if you use stack objects to manage resources, you will have to explicitly call the destructor code before calling exit. However, the destructors of static and global objects will be called.

The quick_exit function causes normal shutdown, but it does not call any destructors nor flush any streams, so there is no resource clean up. The functions registered with atexit are not called, but you can register that termination functions are called by registering them with the at_quick_exit function. After calling these termination functions, the quick_exit function calls the _Exit function that shuts down the process.

You can also call the terminate function to close down a process with no clean up. This process will call a function that has been registered with the set_terminate function and then calls the abort function. If an exception occurs in the program and is not caught--and hence propagates to the main function - the C++ infrastructure will call the terminate function. The abort function is the most severe of mechanisms that terminate a process. This function will exit the process without calling the destructors of objects or performing any other clean up. The function raises the SIGABORT signal and so it is possible to register a function with the signal function, which will be called before the process terminates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset