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
wilder
David Faure 11 years ago
parent 731a9b83f4
commit ee599bfa17
  1. 1
      autotests/CMakeLists.txt
  2. 62
      autotests/ksharedconfig_in_global_object.cpp
  3. 20
      src/core/kconfig.cpp

@ -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
)

@ -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();
}

@ -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;
}

Loading…
Cancel
Save