From 1b91a4779c21735ef0d824337cd67aa3154745db Mon Sep 17 00:00:00 2001
From: Andreas Schenk <andreas_schenk@hms.harvard.edu>
Date: Mon, 13 Feb 2012 18:52:00 -0500
Subject: [PATCH] Fixed gosty application shutdown. Now both the Qt aboutToQuit
 signal and the python atexit work as expected.

---
 modules/gui/src/main.cc                                   | 4 +++-
 modules/gui/src/messages/log_reader.cc                    | 2 --
 modules/gui/src/python_shell/python_interpreter_worker.cc | 6 ++++++
 modules/gui/src/python_shell/python_interpreter_worker.hh | 1 +
 4 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/modules/gui/src/main.cc b/modules/gui/src/main.cc
index 61d6b05d8..f3b1c6f6e 100644
--- a/modules/gui/src/main.cc
+++ b/modules/gui/src/main.cc
@@ -93,7 +93,9 @@ void GostyMainWindow::OnQuit()
   //SetFullScreen(false);
   GostyApp::Instance()->OnQuit();
   deleteLater();
-  QApplication::exit(0);
+  // exit has to be called on the instance not the class,
+  // otherwise the aboutToQuit singal doesn't get emitted
+  QApplication::instance()->exit(0);
 }
 
 void GostyMainWindow::closeEvent(QCloseEvent * event)
diff --git a/modules/gui/src/messages/log_reader.cc b/modules/gui/src/messages/log_reader.cc
index 3562df102..33ad612ba 100644
--- a/modules/gui/src/messages/log_reader.cc
+++ b/modules/gui/src/messages/log_reader.cc
@@ -72,8 +72,6 @@ QMessageBox::Icon LogReader::GetIconForSeverity(int severity){
 }
 
 LogReader::~LogReader() {
-  ost::Logger& logger = ost::Logger::Instance();
-  logger.PopSink();
 }
 
 }
diff --git a/modules/gui/src/python_shell/python_interpreter_worker.cc b/modules/gui/src/python_shell/python_interpreter_worker.cc
index b89859142..47dbcfc9e 100644
--- a/modules/gui/src/python_shell/python_interpreter_worker.cc
+++ b/modules/gui/src/python_shell/python_interpreter_worker.cc
@@ -51,6 +51,12 @@ PythonInterpreterWorker::PythonInterpreterWorker():
   main_namespace_["sys"].attr("stdout") = output_redirector_;
 }
 
+PythonInterpreterWorker::~PythonInterpreterWorker()
+{
+  // we have to manually run the exit functions because we cannot use Py_Finalize due to some problems in boost python
+  run_command_(std::pair<unsigned int, QString>(0,"import atexit\natexit._run_exitfuncs()\n"));
+}
+
 void PythonInterpreterWorker::Wake()
 {
   if (awake_) return;
diff --git a/modules/gui/src/python_shell/python_interpreter_worker.hh b/modules/gui/src/python_shell/python_interpreter_worker.hh
index 4c36b2750..ed80f6fe7 100644
--- a/modules/gui/src/python_shell/python_interpreter_worker.hh
+++ b/modules/gui/src/python_shell/python_interpreter_worker.hh
@@ -20,6 +20,7 @@ class PythonInterpreterWorker: public QObject
 Q_OBJECT
 public:
   PythonInterpreterWorker();
+  ~PythonInterpreterWorker();
   unsigned int AddCommand(const QString& command);
 
 signals:
-- 
GitLab