From 9ad273d83454851d5ab112905db721d50554dcb6 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Mon, 20 Apr 2026 22:19:56 +0200 Subject: [PATCH] Fixing a segfault on 'xkill' - solution was to register an exit handler that cleanly shuts down --- src/lay/lay/layApplication.cc | 10 ++++++++++ src/lay/lay/layApplication.h | 10 +++++++++- src/lay/lay/layMainWindow.cc | 1 - src/lay/lay/layMainWindow.h | 1 - src/laybasic/laybasic/layDispatcher.h | 3 ++- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/lay/lay/layApplication.cc b/src/lay/lay/layApplication.cc index 63b6450cf..259a272b0 100644 --- a/src/lay/lay/layApplication.cc +++ b/src/lay/lay/layApplication.cc @@ -1658,6 +1658,12 @@ GuiApplication::event (QEvent *event) return QApplication::event(event); } +static void atexit_handler () +{ + if (lay::ApplicationBase::instance ()) { + lay::ApplicationBase::instance ()->shutdown (); + } +} int GuiApplication::exec () @@ -1693,6 +1699,10 @@ GuiApplication::exec () } + // register an exit handler to shutdown cleanly in case of an explicit exit + // inside the code + ::atexit (&atexit_handler); + return QApplication::exec (); } diff --git a/src/lay/lay/layApplication.h b/src/lay/lay/layApplication.h index e268df204..2a1322dfe 100644 --- a/src/lay/lay/layApplication.h +++ b/src/lay/lay/layApplication.h @@ -105,6 +105,15 @@ public: */ void exit (int result); + /** + * @brief Shut down the application + * + * Calling this function will close the main window and + * prepare for exit. Unlike "exit", it dows not actually exit + * the process. + */ + virtual void shutdown (); + /** * @brief Return the program's version */ @@ -330,7 +339,6 @@ public: protected: virtual void setup () = 0; - virtual void shutdown (); virtual void prepare_recording (const std::string >f_record, bool gtf_record_incremental); virtual void start_recording (); virtual lay::Dispatcher *dispatcher () const = 0; diff --git a/src/lay/lay/layMainWindow.cc b/src/lay/lay/layMainWindow.cc index 13c76abcf..f63026e95 100644 --- a/src/lay/lay/layMainWindow.cc +++ b/src/lay/lay/layMainWindow.cc @@ -165,7 +165,6 @@ show_dock_widget (QDockWidget *dock_widget, bool visible) MainWindow::MainWindow (QApplication *app, const char *name, bool undo_enabled) : QMainWindow (0), - tl::Object (), lay::DispatcherDelegate (), m_dispatcher (this), m_text_progress (this, 10 /*verbosity threshold*/), diff --git a/src/lay/lay/layMainWindow.h b/src/lay/lay/layMainWindow.h index e1fc24972..a22ac7bc9 100644 --- a/src/lay/lay/layMainWindow.h +++ b/src/lay/lay/layMainWindow.h @@ -94,7 +94,6 @@ class ProgressWidget; */ class LAY_PUBLIC MainWindow : public QMainWindow, - public tl::Object, public gsi::ObjectBase, public lay::DispatcherDelegate { diff --git a/src/laybasic/laybasic/layDispatcher.h b/src/laybasic/laybasic/layDispatcher.h index f42b60585..df26686d8 100644 --- a/src/laybasic/laybasic/layDispatcher.h +++ b/src/laybasic/laybasic/layDispatcher.h @@ -49,6 +49,7 @@ class ConfigureAction; * @brief A delegate by which the dispatcher can submit notification events */ class LAYBASIC_PUBLIC DispatcherDelegate + : public tl::Object { public: /** @@ -271,7 +272,7 @@ private: #if defined(HAVE_QT) QWidget *mp_menu_parent_widget; #endif - DispatcherDelegate *mp_delegate; + tl::weak_ptr mp_delegate; }; }