使用C ++ atexit()函数发生奇怪的崩溃

时间:2014-05-10 18:21:04

标签: c++ crash cout ostream atexit

我在Visual Studio 2013中使用MSVC。这是我正在编译的代码:

#include <iostream>
using namespace std;

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

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

现在的样子 - 它就像一个魅力。我启动程序,它进入崩溃功能,暂停,我按一个键,它正常关闭。一切都很酷。但是,如果我取消注释while循环并使用控制台上的X按钮关闭它,我会在endl函数中出现崩溃。我能够确定崩溃是由_Ostr.widen()引起的 这是由MSVC提供的endl函数的实现:

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

使用Ctrl + C终止程序会产生相同的效果。我该如何解决这个问题?

1 个答案:

答案 0 :(得分: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;
}

请注意:使用系统(&#34; PAUSE&#34;)和忙碌等待只是为了保持示例简单。我建议在实际代码中使用它们。