Global object destructor can throw exception

A C++ destructor can throw an exception and there is at least one static/global instance of this class (or a class derived from this class).

This is unsafe usage because it can cause an exception to be thrown in a context where exceptions are not allowed, such as the static object tear down performed when an executable or a dynamic library is unloaded. If an exception were thrown in such a context, the C++ runtime would cause the application to fail.

ID

Observation

Description

1

Exception throw

This shows where the exception was thrown

2

Definition

This shows where the destructor was defined

Example

          
#include <stdio.h>

class Bomb { // bad class throws exception from destructor
public:
int x;
    Bomb() : x(0) {}
    ~Bomb() { throw "boom"; }
};

// Global variable that will throw exception when
// torn down during program exit
Bomb myBomb;

int main(int argc, char **argv)
{
    printf("goodbye, world\n");
    // program blows up after return from main
}
        

Copyright © 2010, Intel Corporation. All rights reserved.