From 300482b4d560d579444ae2f66ea256e4c8ac3577 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 10 Oct 2021 08:03:34 +0300 Subject: [PATCH 50/50] Qt: Fix segfault when quitting from main menu without visiting game Writing shortcuts crashed if attempted before they had been even initialized. Now check if they are initialized, and don't even try to write them if they are not. See osdn #43019 Signed-off-by: Marko Lindqvist --- client/gui-qt/shortcuts.cpp | 22 ++++++++++++++++++++-- client/gui-qt/shortcuts.h | 7 ++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/client/gui-qt/shortcuts.cpp b/client/gui-qt/shortcuts.cpp index 1473c9f550..e0c7a3dca5 100644 --- a/client/gui-qt/shortcuts.cpp +++ b/client/gui-qt/shortcuts.cpp @@ -250,6 +250,7 @@ shortcut_id fc_shortcuts::get_id(fc_shortcut *sc) void fc_shortcuts::set_shortcut(fc_shortcut *s) { fc_shortcut *sc; + sc = hash.value(s->id); sc->key = s->key; sc->mod = s->mod; @@ -263,7 +264,7 @@ void fc_shortcuts::drop() { if (m_instance) { delete m_instance; - m_instance = 0; + m_instance = nullptr; } } @@ -272,11 +273,22 @@ void fc_shortcuts::drop() **************************************************************************/ fc_shortcuts *fc_shortcuts::sc() { - if (!m_instance) + if (!m_instance) { m_instance = new fc_shortcuts; + } + return m_instance; } +/**********************************************************************//** + Check, without instantiating it in the process, if shortcuts system + has been instantiated already. +**************************************************************************/ +bool fc_shortcuts::is_instantiated() +{ + return m_instance != nullptr; +} + /**********************************************************************//** Inits defaults shortcuts or reads from settings **************************************************************************/ @@ -785,6 +797,12 @@ void write_shortcuts() QSettings s(QSettings::IniFormat, QSettings::UserScope, sname); + if (!fc_shortcuts::is_instantiated()) { + // Shortcuts not even initialized yet. Happens e.g. when we quit from + // the main menu without ever visiting the game. + return; + } + s.beginWriteArray("Shortcuts"); for (int i = 0; i < num_shortcuts; ++i) { s.setArrayIndex(i); diff --git a/client/gui-qt/shortcuts.h b/client/gui-qt/shortcuts.h index a916b11f56..678deeb1b2 100644 --- a/client/gui-qt/shortcuts.h +++ b/client/gui-qt/shortcuts.h @@ -116,16 +116,17 @@ class fc_shortcuts { Q_DISABLE_COPY(fc_shortcuts); fc_shortcuts(); - static fc_shortcuts* m_instance; + static fc_shortcuts *m_instance; public: ~fc_shortcuts(); - static fc_shortcuts* sc(); + static fc_shortcuts *sc(); + static bool is_instantiated(); static void drop(); static QMap hash; public: static void init_default(bool read); - fc_shortcut* get_shortcut(shortcut_id id); + fc_shortcut *get_shortcut(shortcut_id id); shortcut_id get_id(fc_shortcut *sc); void set_shortcut(fc_shortcut *sc); QString get_desc(shortcut_id id); -- 2.33.0