From 446e23af5a5fc1ff432b2435fd24da9bff537331 Mon Sep 17 00:00:00 2001 From: David Edmundson Date: Tue, 15 Oct 2019 12:40:55 +0100 Subject: [PATCH] [aurorae] Fix crash on KCM teardown with Qt5.14 Summary: Context behaviour subtly changed in Qt5.14 which has exposed some questionable things in this KCM. Auorae::Decoration is a context property of the loaded QML. QObject::~QObject signals it's own deletion before deleting children. This means the bindings of loaded QML update in particular aurorae.qml DecorationOptions { id: options deco: decoration } DecorationOptions has a pointer to the previously set decoration which is now dangling, and we crash. Rather than adding more guards and smart pointers in DecorationOptions, this patch deletes the QQmlContext before deleting the context properties that we expose. It also moves unreferring the shared qmlcontext we inherit from till after we've deleted the child context, because it didn't make sense. Test Plan: Opened KCM Closed the KCM didn't crash Reviewers: #kwin, zzag Reviewed By: #kwin, zzag Subscribers: kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D24594 --- plugins/kdecorations/aurorae/src/aurorae.cpp | 18 ++++++++++-------- plugins/kdecorations/aurorae/src/aurorae.h | 4 +++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/plugins/kdecorations/aurorae/src/aurorae.cpp b/plugins/kdecorations/aurorae/src/aurorae.cpp index fd723a8402..f3ad059616 100644 --- a/plugins/kdecorations/aurorae/src/aurorae.cpp +++ b/plugins/kdecorations/aurorae/src/aurorae.cpp @@ -266,17 +266,19 @@ Decoration::Decoration(QObject *parent, const QVariantList &args) Decoration::~Decoration() { - Helper::instance().unref(); if (m_context) { m_context->makeCurrent(m_offscreenSurface.data()); delete m_renderControl; delete m_view.data(); m_fbo.reset(); - delete m_item; m_context->doneCurrent(); } + // deleted explicitly before our own qobject destructor as "this" is a context property of m_qmlContext, + // and changing contextProperties is a bad idea + delete m_qmlContext; + Helper::instance().unref(); } void Decoration::init() @@ -285,9 +287,9 @@ void Decoration::init() auto s = settings(); connect(s.data(), &KDecoration2::DecorationSettings::reconfigured, this, &Decoration::configChanged); - QQmlContext *context = new QQmlContext(Helper::instance().rootContext(), this); - context->setContextProperty(QStringLiteral("decoration"), this); - context->setContextProperty(QStringLiteral("decorationSettings"), s.data()); + m_qmlContext = new QQmlContext(Helper::instance().rootContext(), this); + m_qmlContext->setContextProperty(QStringLiteral("decoration"), this); + m_qmlContext->setContextProperty(QStringLiteral("decorationSettings"), s.data()); auto component = Helper::instance().component(m_themeName); if (!component) { return; @@ -310,9 +312,9 @@ void Decoration::init() connect(this, &Decoration::configChanged, theme, readButtonSize); readButtonSize(); // m_theme->setTabDragMimeType(tabDragMimeType()); - context->setContextProperty(QStringLiteral("auroraeTheme"), theme); + m_qmlContext->setContextProperty(QStringLiteral("auroraeTheme"), theme); } - m_item = qobject_cast< QQuickItem* >(component->create(context)); + m_item = qobject_cast< QQuickItem* >(component->create(m_qmlContext)); if (!m_item) { if (component->isError()) { const auto errors = component->errors(); @@ -322,7 +324,7 @@ void Decoration::init() } return; } - m_item->setParent(this); + m_item->setParent(m_qmlContext); QVariant visualParent = property("visualParent"); if (visualParent.isValid()) { diff --git a/plugins/kdecorations/aurorae/src/aurorae.h b/plugins/kdecorations/aurorae/src/aurorae.h index cb41be1eb5..a08b87a1e6 100644 --- a/plugins/kdecorations/aurorae/src/aurorae.h +++ b/plugins/kdecorations/aurorae/src/aurorae.h @@ -27,6 +27,7 @@ class QOffscreenSurface; class QOpenGLContext; class QOpenGLFramebufferObject; class QQmlComponent; +class QQmlContext; class QQmlEngine; class QQuickItem; class QQuickRenderControl; @@ -82,7 +83,8 @@ private: QImage m_buffer; QRect m_contentRect; //the geometry of the part of the buffer that is not a shadow when buffer was created. QPointer m_view; - QQuickItem *m_item; + QQuickItem *m_item = nullptr; + QQmlContext *m_qmlContext = nullptr; KWin::Borders *m_borders; KWin::Borders *m_maximizedBorders; KWin::Borders *m_extendedBorders;