diff --git a/src/PowderToy.cpp b/src/PowderToy.cpp index d6f37ef5c..685a93f79 100644 --- a/src/PowderToy.cpp +++ b/src/PowderToy.cpp @@ -123,22 +123,30 @@ void BlueScreen(String detailMessage) } } +struct +{ + int sig; + const char *message; +} signalMessages[] = { + { SIGSEGV, "Memory read/write error" }, + { SIGFPE, "Floating point exception" }, + { SIGILL, "Program execution exception" }, + { SIGABRT, "Unexpected program abort" }, + { 0, nullptr }, +}; + void SigHandler(int signal) { - switch(signal){ - case SIGSEGV: - BlueScreen("Memory read/write error"); - break; - case SIGFPE: - BlueScreen("Floating point exception"); - break; - case SIGILL: - BlueScreen("Program execution exception"); - break; - case SIGABRT: - BlueScreen("Unexpected program abort"); - break; + const char *message = "Unknown signal"; + for (auto *msg = signalMessages; msg->message; ++msg) + { + if (msg->sig == signal) + { + message = msg->message; + break; + } } + BlueScreen(ByteString(message).FromUtf8()); } constexpr int SCALE_MAXIMUM = 10; @@ -176,6 +184,11 @@ int main(int argc, char * argv[]) { Platform::SetupCrt(); Platform::Atexit([]() { + // Unregister dodgy error handlers so they don't try to show the blue screen when the window is closed + for (auto *msg = signalMessages; msg->message; ++msg) + { + signal(msg->sig, SIG_DFL); + } SDLClose(); explicitSingletons.reset(); }); @@ -381,10 +394,10 @@ int main(int argc, char * argv[]) if (enableBluescreen) { //Get ready to catch any dodgy errors - signal(SIGSEGV, SigHandler); - signal(SIGFPE, SigHandler); - signal(SIGILL, SigHandler); - signal(SIGABRT, SigHandler); + for (auto *msg = signalMessages; msg->message; ++msg) + { + signal(msg->sig, SigHandler); + } } if constexpr (X86)