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.