From 491f546463df18c9d8831d803b6382bdd05d7622 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Fri, 24 Jun 2022 19:17:53 +0200 Subject: Don't inherit from containers Containers (both Qt and STL) don't have virtual destructors, which could be an issue when trying to delete an object of a derived class via a pointer to the base class. --- src/core/ksharedconfig.cpp | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'src/core/ksharedconfig.cpp') diff --git a/src/core/ksharedconfig.cpp b/src/core/ksharedconfig.cpp index 09cd35f1..5d0661aa 100644 --- a/src/core/ksharedconfig.cpp +++ b/src/core/ksharedconfig.cpp @@ -16,10 +16,12 @@ void _k_globalMainConfigSync(); -class GlobalSharedConfigList : public QList +using SharedConfigList = QList; + +class GlobalSharedConfig { public: - GlobalSharedConfigList() + GlobalSharedConfig() : wasTestModeEnabled(false) { if (!qApp || QThread::currentThread() == qApp->thread()) { @@ -33,13 +35,14 @@ public: // the thread exits. } + SharedConfigList configList; // in addition to the list, we need to hold the main config, // so that it's not created and destroyed all the time. KSharedConfigPtr mainConfig; bool wasTestModeEnabled; }; -static QThreadStorage s_storage; +static QThreadStorage s_storage; template T *perThreadGlobalStatic() { @@ -50,14 +53,14 @@ T *perThreadGlobalStatic() } // Q_GLOBAL_STATIC(GlobalSharedConfigList, globalSharedConfigList), but per thread: -static GlobalSharedConfigList *globalSharedConfigList() +static GlobalSharedConfig *globalSharedConfig() { - return perThreadGlobalStatic(); + return perThreadGlobalStatic(); } void _k_globalMainConfigSync() { - KSharedConfigPtr mainConfig = globalSharedConfigList()->mainConfig; + KSharedConfigPtr mainConfig = globalSharedConfig()->mainConfig; if (mainConfig) { mainConfig->sync(); } @@ -66,19 +69,19 @@ void _k_globalMainConfigSync() KSharedConfigPtr KSharedConfig::openConfig(const QString &_fileName, OpenFlags flags, QStandardPaths::StandardLocation resType) { QString fileName(_fileName); - GlobalSharedConfigList *list = globalSharedConfigList(); + GlobalSharedConfig *global = globalSharedConfig(); if (fileName.isEmpty() && !flags.testFlag(KConfig::SimpleConfig)) { // Determine the config file name that KConfig will make up (see KConfigPrivate::changeFileName) fileName = KConfig::mainConfigName(); } - if (!list->wasTestModeEnabled && QStandardPaths::isTestModeEnabled()) { - list->wasTestModeEnabled = true; - list->clear(); - list->mainConfig = nullptr; + if (!global->wasTestModeEnabled && QStandardPaths::isTestModeEnabled()) { + global->wasTestModeEnabled = true; + global->configList.clear(); + global->mainConfig = nullptr; } - for (auto *cfg : std::as_const(*list)) { + for (auto *cfg : std::as_const(global->configList)) { if (cfg->name() == fileName && cfg->d_ptr->openFlags == flags && cfg->locationType() == resType // cfg->backend()->type() == backend ) { @@ -89,7 +92,7 @@ KSharedConfigPtr KSharedConfig::openConfig(const QString &_fileName, OpenFlags f KSharedConfigPtr ptr(new KSharedConfig(fileName, flags, resType)); if (_fileName.isEmpty() && flags == FullConfig && resType == QStandardPaths::GenericConfigLocation) { - list->mainConfig = ptr; + global->mainConfig = ptr; const bool isMainThread = !qApp || QThread::currentThread() == qApp->thread(); static bool userWarned = false; @@ -123,13 +126,13 @@ KSharedConfig::Ptr KSharedConfig::openStateConfig(const QString &_fileName) KSharedConfig::KSharedConfig(const QString &fileName, OpenFlags flags, QStandardPaths::StandardLocation resType) : KConfig(fileName, flags, resType) { - globalSharedConfigList()->append(this); + globalSharedConfig()->configList.append(this); } KSharedConfig::~KSharedConfig() { if (s_storage.hasLocalData()) { - globalSharedConfigList()->removeAll(this); + globalSharedConfig()->configList.removeAll(this); } } -- cgit v1.2.1