c++的atexit()函数奇怪的崩溃

Strange crash with C++ atexit() function

本文关键字:崩溃 函数 atexit c++      更新时间:2023-10-16

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

#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函数内部就会崩溃。我能够确定崩溃是由_ostrs .拓宽()引起的。这是endl函数的实现,由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);
    }

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

看来我的怀疑是真的。我这样修改代码:

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

当程序正常存在时,两个printf()s显示相同的线程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")和busy waiting只是为了使示例保持简单。我不建议在实际代码中使用。