Search code examples
c++visual-c++terminateundefined-behavior

Is null terminate() handler allowed?


In VC++7 if I do the following:

void myTerminate()
{
    cout << "In myTerminate()";
    abort();
}

int main( int, char** )
{
    set_terminate( &myTerminate );
    set_terminate( 0 );
    terminate();
    return 0;
}

the program behaves exactly as if abort() was called directly which is exactly what default terminate() handler does.

If I omit the set_terminate( 0 ); statement my terminate handler is being called. So calling set_terminate( 0 ) seems to have the effect of resetting the terminate() handler to default.

Is this behavior specific to VC++7 only? Won't the program run into undefined behavior if I call set_terminate( 0 ) on some other implementation?


Solution

  • Looking into the standard reveals the following:

    terminate_handler set_terminate(terminate_handler f) throw();
    

    1 Effects: Establishes the function designated by f as the current handler function ... cut
    2 Requires: f shall not be a null pointer.
    3 Returns: The previous terminate_handler.

    Seems to be non-standard.