From 2f902694656d67d671d9a8616060d598a478e7b7 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 8 Oct 2015 22:27:16 +0200 Subject: [PATCH] Fix crash in theme code when loading engines in succession Store global engine specific themes as dynamic properties on the engines themselves. This avoids the problem that storing engines and their themes to a hash map had that destroyed engines and themes were not cleaned up from the hash map. Change-Id: I7330995339c263d456f74d33f6535d5ac7d9d5d4 Task-number: QTBUG-48651 Reviewed-by: Mitch Curtis Reviewed-by: Liang Qi --- .gitignore | 1 + src/imports/controls/qquicktheme.cpp | 11 ++-- tests/auto/auto.pro | 3 +- .../{controls => theme}/data/tst_theme.qml | 0 tests/auto/theme/theme.pro | 12 ++++ tests/auto/theme/tst_theme.cpp | 57 +++++++++++++++++++ 6 files changed, 78 insertions(+), 6 deletions(-) rename tests/auto/{controls => theme}/data/tst_theme.qml (100%) create mode 100644 tests/auto/theme/theme.pro create mode 100644 tests/auto/theme/tst_theme.cpp diff --git a/.gitignore b/.gitignore index 311c2751f6..2f84640646 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ /tests/auto/controls/tst_controls /tests/auto/sanity/tst_sanity /tests/auto/snippets/tst_snippets +/tests/auto/theme/tst_theme /tests/benchmarks/creationtime/tst_creationtime /tests/benchmarks/objectcount/tst_objectcount diff --git a/src/imports/controls/qquicktheme.cpp b/src/imports/controls/qquicktheme.cpp index 395d60d783..7fec28c945 100644 --- a/src/imports/controls/qquicktheme.cpp +++ b/src/imports/controls/qquicktheme.cpp @@ -102,11 +102,12 @@ Q_GLOBAL_STATIC_WITH_ARGS(QQuickThemeData, globalThemeData, (QString::fromLatin1 static QQuickThemeAttached *themeInstance(QQmlEngine *engine) { - static QHash themes; - QHash::iterator it = themes.find(engine); - if (it == themes.end()) - it = themes.insert(engine, new QQuickThemeAttached(*globalThemeData(), engine)); - return it.value(); + QQuickThemeAttached *theme = engine->property("_q_quicktheme").value(); + if (!theme) { + theme = new QQuickThemeAttached(*globalThemeData(), engine); + engine->setProperty("_q_quicktheme", QVariant::fromValue(theme)); + } + return theme; } static QQuickThemeAttached *attachedTheme(QObject *object) diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 2d12e08cf8..ff3d32e1ec 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -6,4 +6,5 @@ SUBDIRS += \ calendar \ controls \ sanity \ - snippets + snippets \ + theme diff --git a/tests/auto/controls/data/tst_theme.qml b/tests/auto/theme/data/tst_theme.qml similarity index 100% rename from tests/auto/controls/data/tst_theme.qml rename to tests/auto/theme/data/tst_theme.qml diff --git a/tests/auto/theme/theme.pro b/tests/auto/theme/theme.pro new file mode 100644 index 0000000000..e8d1133ae1 --- /dev/null +++ b/tests/auto/theme/theme.pro @@ -0,0 +1,12 @@ +TEMPLATE = app +TARGET = tst_theme +CONFIG += qmltestcase + +SOURCES += \ + $$PWD/tst_theme.cpp + +OTHER_FILES += \ + $$PWD/data/* + +TESTDATA += \ + $$PWD/data/tst_* diff --git a/tests/auto/theme/tst_theme.cpp b/tests/auto/theme/tst_theme.cpp new file mode 100644 index 0000000000..daa1b1fe16 --- /dev/null +++ b/tests/auto/theme/tst_theme.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +// based on QUICK_TEST_MAIN() defined in quicktest.h +int main(int argc, char **argv) +{ + QTEST_ADD_GPU_BLACKLIST_SUPPORT + QTEST_SET_MAIN_SOURCE_PATH + + { + // QTBUG-48651 + QGuiApplication app(argc, argv); + QQmlApplicationEngine engine; + engine.loadData("import Qt.labs.controls 1.0; Slider { } "); + if (engine.rootObjects().isEmpty()) + return -1; + } + + return quick_test_main(argc, argv, "tst_theme", QUICK_TEST_SOURCE_DIR); +}