fix: fix not thread-safe settings singleton.

fix not thread-safe way for fetching settings singleton.
remove qApp from Settings' parent and leak it anyway.
This commit is contained in:
2026-07-05 08:54:50 +08:00
parent 1de1517a81
commit 99378a8ccd
2 changed files with 11 additions and 16 deletions
+10 -13
View File
@@ -13,6 +13,14 @@
#include <QKeySequence>
#include <QMetaEnum>
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 <typename E>
@@ -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;
+1 -3
View File
@@ -51,9 +51,7 @@ public:
void setHiDpiScaleFactorBehavior(Qt::HighDpiScaleFactorRoundingPolicy hidpi);
private:
Settings();
static Settings *m_settings_instance;
Settings(QObject *parent = nullptr);
QSettings *m_qsettings;