Can a Page Fault Lead to Faulting Process Being Terminated

  • Download source code - 12.five KB

Introduction

This article describes the standard techniques of handling exceptions and errors in Visual C++ programs running in Windows.

An exception (or a disquisitional mistake, or crash) typically means your program stops working normally and needs to terminate its execution. For case, an exception may occur because of your programme accessing an invalid retentivity accost (such equally Goose egg pointer), memory buffer can't be allocated (out of memory), the C run-time libraries (CRT) detect an mistake and request programme termination, and and then on.

A C++ program may handle several types of exceptions: SEH exceptions produced through the Operating System's structured exception handling mechanism, CRT errors produced by the C run-time libraries, and finally, signals. Each error type requires installing an exception handler function that would intercept an exception and practice some fault recovery actions.

If your application has several execution threads, things may be even more complicated. Some exception handlers work for an entire process, but some piece of work for the current thread simply. And then you have to install exception handlers in each thread.

Each module (EXE or DLL) in your application is linked to CRT libraries (either statically or dynamically). Exception handling techniques strongly depend on the CRT linkage blazon.

The multifariousness of mistake types, differences of treatment exceptions in multi-threaded programs, and dependence of exception handling on CRT linkage requires a lot of work to handle exactly all exceptions that your application is allowed to handle. This article is aimed to help you better empathize the exception treatment mechanisms and effectively use exception treatment in your C++ applications.

A small console demo application ExceptionHandler is attached to the article. The demo can raise and take hold of different types of exceptions and generate a crash minidump file allowing to run into the line of the code where exception occurred.

Groundwork

Some time agone I needed a mode for intercepting exceptions for one of my Open up-Source projects, CrashRpt - A crash reporting library for Windows applications. The CrashRpt library handles exceptions occurring in your awarding, collects technical information almost the mistake (such as crash minidump, error logs, desktop screenshots), and offers the user to send an error report over the Cyberspace (effigy one).

Figure i - Error report window and error report details dialogs of the CrashRpt library

crashrpt.png

Maybe you have seen the Windows Error Reporting window (figure two) suddenly actualization on your desktop, and the CrashRpt library does the same things, except it sends the mistake written report to your own web-server instead of Microsoft's server.

Figure 2 - Windows Fault Reporting (Dr. Watson) window

wer.gif

Browsing the MSDN gave me the SetUnhandledExceptionFilter() function that I used to handle admission violations. Simply before long I figured out that some of exceptions in my application are somehow left unhandled and Dr. Watson's window still appears instead of the CrashRpt window.

I browsed MSDN some more and institute many other CRT-provided functions that could exist used to handle CRT errors. Here are some examples of such functions: set_terminate(), _set_invalid_parameter_handler(), _set_purecall_handler().

And then I establish that some CRT handlers are valid for the current thread only, but some of them work for all threads of the procedure.

Standing my research, I plant out that there are many nuances a developer must empathise to utilize exception handling finer. Results of my research are presented below.

Some Words About Exceptions

As you already know, an exception or a critical error typically means that a program stops working normally and needs to stop its execution.

For example, an exception may occur because of the following:

  • program accesses an invalid retentivity address (such equally Cypher pointer)
  • stack overflows due to infinite recursion
  • large block of information is written to a minor buffer
  • a pure virtual method of a C++ form is called
  • memory buffer tin can't exist allocated (out of memory)
  • invalid parameter is passed to a C++ organisation role
  • C run-time libraries find an error and request program termination

There are two kinds of exceptions that accept a different nature: SEH exceptions (Structured Exception Handling, SEH) and typed C++ exceptions. You can find an in-depth clarification of exception mechanism implementation in the excellent How a Compiler Implements Exception Handling article by Vishal Kochhar.

Structured exception treatment mechanism is provided by the Operating System (this means that all Windows applications can raise and handle SEH exceptions). SEH exceptions were originally designed for the C language, merely they can exist used in C++ likewise.

SEH exceptions are handled using the __try{}__except(){} construction. The main() function of your program is guarded with such a construction, so past default all unhandled SEH exceptions are caught and Dr. Watson is invoked. SEH exceptions are Visual C++ compiler-specific. If you write portable code, you should guard the structured exception treatment construction with #ifdef/

0 Response to "Can a Page Fault Lead to Faulting Process Being Terminated"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel