diff options
author | David Faure <faure@kde.org> | 2015-04-04 13:40:21 +0200 |
---|---|---|
committer | David Faure <faure@kde.org> | 2015-04-04 13:44:01 +0200 |
commit | ee599bfa17f7e56b0db1a4afbbfe929ec90f2c9d (patch) | |
tree | eef98e45e0714e13dde055e9a684290588d97dc8 | |
parent | 731a9b83f4bd1f2dc840cfae84edcd0b533810aa (diff) | |
download | kconfig-ee599bfa17f7e56b0db1a4afbbfe929ec90f2c9d.tar.gz kconfig-ee599bfa17f7e56b0db1a4afbbfe929ec90f2c9d.tar.bz2 |
KConfig: fix using KSharedConfig in global object destructor.
ksharedconfig_in_global_object.cpp is now in kdelibs4 too
(where it works) and reproduces Albert's KgDifficulty testcase.
CHANGELOG: fix assert when using KSharedConfig in a global object destructor.
REVIEW: 122232
-rw-r--r-- | autotests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | autotests/ksharedconfig_in_global_object.cpp | 62 | ||||
-rw-r--r-- | src/core/kconfig.cpp | 20 |
3 files changed, 79 insertions, 4 deletions
diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index b91f754b..69e23ac4 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -37,6 +37,7 @@ ecm_add_tests( kdesktopfiletest.cpp ksharedconfigtest.cpp test_kconf_update.cpp + ksharedconfig_in_global_object NAME_PREFIX kconfigcore- LINK_LIBRARIES KF5::ConfigCore Qt5::Test Qt5::Concurrent ) diff --git a/autotests/ksharedconfig_in_global_object.cpp b/autotests/ksharedconfig_in_global_object.cpp new file mode 100644 index 00000000..7a4f66bf --- /dev/null +++ b/autotests/ksharedconfig_in_global_object.cpp @@ -0,0 +1,62 @@ +/* This file is part of the KDE project + Copyright (C) 2007 Matthias Kretz <kretz@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. + +*/ + +#include <QtCore/QCoreApplication> +#include <QtCore/QtGlobal> +#include <QTimer> +#include <ksharedconfig.h> +#include <kconfiggroup.h> +#include <stdio.h> + +class Tester +{ +public: + void initConfig(); + ~Tester(); +}; + +void Tester::initConfig() +{ + fprintf(stderr, "app Tester\n"); + KConfigGroup group(KSharedConfig::openConfig(), "test"); + group.writeEntry("test", 0); +} + +Tester::~Tester() +{ + fprintf(stderr, "app ~Tester\n"); + KConfigGroup group(KSharedConfig::openConfig(), "test"); + group.writeEntry("test", 1); +} + +Q_GLOBAL_STATIC(Tester, globalTestObject) + +int main(int argc, char **argv) +{ + qputenv("QT_FATAL_WARNINGS", "1"); + QCoreApplication app(argc, argv); + + KSharedConfig::Ptr config = KSharedConfig::openConfig(); + + Tester *t = globalTestObject(); + t->initConfig(); + + QTimer::singleShot(0, qApp, SLOT(quit())); + return app.exec(); +} diff --git a/src/core/kconfig.cpp b/src/core/kconfig.cpp index 1da816fa..3819716b 100644 --- a/src/core/kconfig.cpp +++ b/src/core/kconfig.cpp @@ -530,23 +530,35 @@ KConfig::OpenFlags KConfig::openFlags() const return d->openFlags; } -Q_GLOBAL_STATIC(QString, globalMainConfigName) +struct KConfigStaticData +{ + QString globalMainConfigName; + // Keep a copy so we can use it in global dtors, after qApp is gone + // This is a workaround for the Qt bug fixed in https://codereview.qt-project.org/107005 for Qt 5.5 + // Revert this addition once we can depend on Qt 5.5. + QStringList appArgs; +}; +Q_GLOBAL_STATIC(KConfigStaticData, globalData) void KConfig::setMainConfigName(const QString &str) { - *globalMainConfigName() = str; + globalData()->globalMainConfigName = str; } QString KConfig::mainConfigName() { + KConfigStaticData* data = globalData(); + if (data->appArgs.isEmpty()) + data->appArgs = QCoreApplication::arguments(); + // --config on the command line overrides everything else - const QStringList args = QCoreApplication::arguments(); + const QStringList args = data->appArgs; for (int i = 1; i < args.count(); ++i) { if (args.at(i) == QLatin1String("--config") && i < args.count() - 1) { return args.at(i + 1); } } - const QString globalName = *globalMainConfigName(); + const QString globalName = data->globalMainConfigName; if (!globalName.isEmpty()) { return globalName; } |