1

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?

4

1 回答 1

1

看来我的猜想变成了真的。我像这样修改了代码:

#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;
}

当程序正常存在时,两个 printf() 都显示相同的线程 ID,但是当我按下 Ctrl+C 或 X 按钮时,线程 ID 不同,这解释了崩溃,并且在您考虑它时很有意义。因此,这是一个如何解决此问题的小示例:

#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;
}

请注意: system("PAUSE") 和忙等待的使用只是为了保持示例简单。我建议在实际代码中使用它们。

于 2014-05-10T19:42:31.207 回答