From 3d5be3a70e34204e82a83dcabb91f5a2e12e8bba Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 10 Oct 2021 08:16:42 +0300 Subject: [PATCH 20/20] 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 | 23 +++++++++++++++++++++-- client/gui-qt/shortcuts.h | 7 ++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/client/gui-qt/shortcuts.cpp b/client/gui-qt/shortcuts.cpp index a3ec1f06ff..08c1b8d2de 100644 --- a/client/gui-qt/shortcuts.cpp +++ b/client/gui-qt/shortcuts.cpp @@ -246,6 +246,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; @@ -259,7 +260,7 @@ void fc_shortcuts::drop() { if (m_instance) { delete m_instance; - m_instance = 0; + m_instance = nullptr; } } @@ -268,11 +269,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 **************************************************************************/ @@ -771,6 +783,13 @@ void write_shortcuts() QMap h = fc_shortcuts::hash; QSettings s(QSettings::IniFormat, QSettings::UserScope, "freeciv-qt-client"); + + 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 52b9c9fc6e..d9b8c13e65 100644 --- a/client/gui-qt/shortcuts.h +++ b/client/gui-qt/shortcuts.h @@ -115,16 +115,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