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?