From 677ee0cf7cf492482b4dd4d96986b93f89cb69b3 Mon Sep 17 00:00:00 2001 From: David Redondo Date: Wed, 9 Apr 2025 16:06:40 +0200 Subject: [PATCH] QGnomeTheme: Prevent leak of QDBusPendingCallWatcher Otherwise the watcher can outlive the theme and the connected lambda calls into the already destroyed theme. Change-Id: I55e7834d1eb806d9f5f1945a4e1016d14b2969b6 Reviewed-by: Axel Spoerl --- src/gui/platform/unix/qgnometheme.cpp | 5 ++--- src/gui/platform/unix/qgnometheme_p.h | 2 ++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/gui/platform/unix/qgnometheme.cpp b/src/gui/platform/unix/qgnometheme.cpp index c70c3580185..aafaff3732b 100644 --- a/src/gui/platform/unix/qgnometheme.cpp +++ b/src/gui/platform/unix/qgnometheme.cpp @@ -66,15 +66,14 @@ QGnomeThemePrivate::QGnomeThemePrivate() message.setArguments({}); message << appearanceNamespace << contrastKey; - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(dbus.asyncCall(message)); - QObject::connect(watcher, &QDBusPendingCallWatcher::finished, watcher, [this](QDBusPendingCallWatcher *watcher) { + pendingCallWatcher = std::make_unique(dbus.asyncCall(message)); + QObject::connect(pendingCallWatcher.get(), &QDBusPendingCallWatcher::finished, pendingCallWatcher.get(), [this](QDBusPendingCallWatcher *watcher) { if (!watcher->isError()) { QDBusPendingReply reply = *watcher; if (Q_LIKELY(reply.isValid())) updateHighContrast(static_cast(reply.value().toUInt())); } initDbus(); - watcher->deleteLater(); }); } else { qCWarning(lcQpaThemeGnome) << "dbus connection failed. Last error: " << dbus.lastError(); diff --git a/src/gui/platform/unix/qgnometheme_p.h b/src/gui/platform/unix/qgnometheme_p.h index 08bba609875..41df765eccf 100644 --- a/src/gui/platform/unix/qgnometheme_p.h +++ b/src/gui/platform/unix/qgnometheme_p.h @@ -24,6 +24,7 @@ QT_BEGIN_NAMESPACE class QGnomeThemePrivate; #if QT_CONFIG(dbus) class QDBusListener; +class QDBusPendingCallWatcher; #endif class Q_GUI_EXPORT QGnomeTheme : public QGenericUnixTheme @@ -66,6 +67,7 @@ public: Qt::ContrastPreference m_contrast = Qt::ContrastPreference::NoPreference; private: std::unique_ptr dbus; + std::unique_ptr pendingCallWatcher; bool initDbus(); void updateColorScheme(const QString &themeName); void updateHighContrast(Qt::ContrastPreference contrast);