From fc3ef1cd7f6facdd060d6ebd226fc43ac938f4e5 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sun, 5 Jul 2026 08:54:50 +0800 Subject: [PATCH] fix: fix not thread-safe settings singleton. fix not thread-safe way for fetching settings singleton. remove qApp from Settings' parent, use nullptr replace it and leak it anyway. because we may initialize it before initializing QApplication. in that time, qApp is nullptr. --- app/settings.cpp | 23 ++++++++++------------- app/settings.h | 4 +--- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/app/settings.cpp b/app/settings.cpp index fa0a285..81165c0 100644 --- a/app/settings.cpp +++ b/app/settings.cpp @@ -13,6 +13,14 @@ #include #include +Settings *Settings::instance() +{ + // SAFETY: + // This is a global singleton and it is leaked by design. + static Settings* INSTANCE = new Settings(); + return INSTANCE; +} + namespace QEnumHelper { template @@ -34,17 +42,6 @@ namespace QEnumHelper } } -Settings *Settings::m_settings_instance = nullptr; - -Settings *Settings::instance() -{ - if (!m_settings_instance) { - m_settings_instance = new Settings; - } - - return m_settings_instance; -} - bool Settings::useLightCheckerboard() const { return m_qsettings->value("use_light_checkerboard", false).toBool(); @@ -156,8 +153,8 @@ QString getApplicationDirPath() } #endif // defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN) -Settings::Settings() - : QObject(qApp) +Settings::Settings(QObject *parent) + : QObject(parent) { QString configPath; diff --git a/app/settings.h b/app/settings.h index fc74b3f..3552155 100644 --- a/app/settings.h +++ b/app/settings.h @@ -51,9 +51,7 @@ public: void setHiDpiScaleFactorBehavior(Qt::HighDpiScaleFactorRoundingPolicy hidpi); private: - Settings(); - - static Settings *m_settings_instance; + Settings(QObject *parent = nullptr); QSettings *m_qsettings;