diff --git a/ApplicationCode/Application/RiaApplication.cpp b/ApplicationCode/Application/RiaApplication.cpp index 2f208c4974..83ed4cddc4 100644 --- a/ApplicationCode/Application/RiaApplication.cpp +++ b/ApplicationCode/Application/RiaApplication.cpp @@ -134,6 +134,40 @@ void AppEnum< RiaApplication::RINavigationPolicy >::setUp() //================================================================================================== +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RiaApplication::notify(QObject* receiver, QEvent* event) +{ + // Pre-allocating a memory exhaustion message + // Doing som e trickery to avoid deadlock, as creating a messagebox actually triggers a call to this notify method. + + static QMessageBox* memoryExhaustedBox = nullptr; + static bool allocatingMessageBox = false; + if (!memoryExhaustedBox && !allocatingMessageBox) + { + allocatingMessageBox = true; + memoryExhaustedBox = new QMessageBox(QMessageBox::Critical, + "ResInsight Exhausted Memory", + "Memory is Exhausted!\n ResInsight could not allocate the memory needed, and is now unstable and will probably crash soon."); + } + + bool done = true; + try + { + done = QApplication::notify(receiver, event); + } + catch ( const std::bad_alloc& ) + { + if (memoryExhaustedBox) memoryExhaustedBox->exec(); + std::cout << "ResInsight: Memory is Exhausted!\n ResInsight could not allocate the memory needed, and is now unstable and will probably crash soon." << std::endl; + // If we really want to crash instead of limping forward: + // throw; + } + + return done; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/Application/RiaApplication.h b/ApplicationCode/Application/RiaApplication.h index a5e611bc9b..f920cadfcd 100644 --- a/ApplicationCode/Application/RiaApplication.h +++ b/ApplicationCode/Application/RiaApplication.h @@ -216,6 +216,8 @@ private: friend RiaArgumentParser; void setHelpText(const QString& helpText); + virtual bool notify(QObject *, QEvent *) override; + private slots: void slotWorkerProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); diff --git a/ApplicationCode/Application/RiaMain.cpp b/ApplicationCode/Application/RiaMain.cpp index 0e6e55a784..0332149550 100644 --- a/ApplicationCode/Application/RiaMain.cpp +++ b/ApplicationCode/Application/RiaMain.cpp @@ -49,7 +49,22 @@ int main(int argc, char *argv[]) RiaLogging::setLoggerInstance(new RiuMessagePanelLogger(window.messagePanel())); RiaLogging::loggerInstance()->setLevel(RI_LL_DEBUG); - int exitCode = app.exec(); + int exitCode = 0; + try + { + exitCode = app.exec(); + } + catch (std::exception& exep ) + { + std::cout << "A standard c++ exception that terminated ResInsight caught in RiaMain.cpp: " << exep.what() << std::endl; + throw; + } + catch(...) + { + std::cout << "An unknown exception that terminated ResInsight caught in RiaMain.cpp. " << std::endl; + throw; + } + RiaLogging::deleteLoggerInstance(); return exitCode;