Search code examples
c++crashcoutostreamatexit

Strange crash with C++ atexit() function


I am using MSVC with Visual Studio 2013. This is the code I am compiling:

#include <iostream>
using namespace std;

void crash()
{
    cout << "crash?" << endl;
    system("PAUSE");
}

int main(int argc, char *argv[])
{
    atexit(crash);
    //while(true);
    return 0;
}

The way it is right now - it works like a charm. I start the program, it goes inside the crash function, pauses, I press a key and it closes normally. All cool. However, if I uncomment the while loop and use the X button on the console to close it, I get a crash inside the endl function. I was able to determine that the crash is caused by _Ostr.widen() This is the implementation of the endl function, provided by MSVC:

template<class _Elem,
    class _Traits> inline
    basic_ostream<_Elem, _Traits>&
        __CLRCALL_OR_CDECL endl(basic_ostream<_Elem, _Traits>& _Ostr)
    {   // insert newline and flush stream
    _Ostr.put(_Ostr.widen('\n'));
    _Ostr.flush();
    return (_Ostr);
    }

Using Ctrl+C to terminate the program causes the same effect. How can I fix this?


Solution

  • Seems like my suspicions turned out to be true. I modified the code like so:

    #include <iostream>
    using namespace std;
    
    #include <Windows.h>
    
    void crash()
    {
        printf("%i\n", GetCurrentThreadId());
        system("PAUSE");
    }
    
    int main()
    {
        printf("%i\n", GetCurrentThreadId());
    
        atexit(crash);
        //while(true);
    
        return 0;
    }
    

    When the program exists normally both printf()s display identical thread IDs, however when I press Ctrl+C or the X button the thread IDs are different, which explains the crash and makes a lot of sense when you think about it. Thus, here is a small example how this issue can be tackled:

    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    #include <Windows.h>
    
    volatile bool wantClose = false;
    
    void OnExit()
    {
        cout << GetCurrentThreadId() << endl;
        system("PAUSE");
    }
    
    BOOL WINAPI OnConsoleClose(DWORD dwCtrlType)
    {
        wantClose = true; // set a flag that the console wants us to close
        ExitThread(0); // kill this thread immediately so it doesn't make the console stuck
        return FALSE;
    }
    
    int main()
    {
        cout << GetCurrentThreadId() << endl;
    
        SetConsoleCtrlHandler(OnConsoleClose, TRUE); // handle close requests from the console
        atexit(OnExit);
        while(!wantClose); // at some point in our code we will have to check whether the console wants us to close down
    
        return 0;
    }
    

    Please note: The usage of system("PAUSE") and busy waiting are only for the sake of keeping the example simple. I do not advise their usage in real code.