From ee6f4388eef3481c11c5f9f724221ccd02dc581b Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 24 Jun 2020 15:47:56 +0100 Subject: [PATCH] Change how fatalMessageBoxThenExit works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This avoids crashing when using Qt : we cannot call exit() safely since QGuiApplication won’t shut down correctly. Instead throw a special marker object and catch this in boostrap. For an instance of this, see: https://sourceforge.net/p/flightgear/codetickets/2070/ --- src/GUI/MessageBox.cxx | 5 ++++- src/GUI/MessageBox.hxx | 6 ++++++ src/Main/bootstrap.cxx | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/GUI/MessageBox.cxx b/src/GUI/MessageBox.cxx index 9730269c8..b824040a2 100644 --- a/src/GUI/MessageBox.cxx +++ b/src/GUI/MessageBox.cxx @@ -191,7 +191,10 @@ MessageBoxResult fatalMessageBoxWithoutExit(const std::string& caption, int exitStatus) { fatalMessageBoxWithoutExit(caption, msg, moreText); - exit(exitStatus); + // we can't use exit() here or QGuiApplication crashes + // let's instead throw a sepcial exception which we catch + // in boostrap. + throw FatalErrorException{}; } } // of namespace flightgear diff --git a/src/GUI/MessageBox.hxx b/src/GUI/MessageBox.hxx index ab4d62e05..d5c3301de 100644 --- a/src/GUI/MessageBox.hxx +++ b/src/GUI/MessageBox.hxx @@ -7,6 +7,12 @@ namespace flightgear { +// special exception class used to signal an exit. Must not inherit +// std::exception or similar, since we want to handle it specially +class FatalErrorException +{ +}; + enum MessageBoxResult { MSG_BOX_OK, diff --git a/src/Main/bootstrap.cxx b/src/Main/bootstrap.cxx index e78a152fb..ebde1ccd5 100644 --- a/src/Main/bootstrap.cxx +++ b/src/Main/bootstrap.cxx @@ -320,9 +320,10 @@ int main ( int argc, char **argv ) flightgear::fatalMessageBoxWithoutExit("Fatal exception", e.what()); } catch (const std::string &s) { flightgear::fatalMessageBoxWithoutExit("Fatal exception", s); + } catch (const flightgear::FatalErrorException&) { + // we already showed the message box, just carry on to exit } catch (const char *s) { std::cerr << "Fatal error (const char*): " << s << std::endl; - } catch (...) { std::cerr << "Unknown exception in the main loop. Aborting..." << std::endl; if (errno)