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 <axel.spoerl@qt.io>
This commit is contained in:
David Redondo 2025-04-09 16:06:40 +02:00
parent ab7eb492cb
commit 677ee0cf7c
2 changed files with 4 additions and 3 deletions

View File

@ -66,15 +66,14 @@ QGnomeThemePrivate::QGnomeThemePrivate()
message.setArguments({}); message.setArguments({});
message << appearanceNamespace << contrastKey; message << appearanceNamespace << contrastKey;
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(dbus.asyncCall(message)); pendingCallWatcher = std::make_unique<QDBusPendingCallWatcher>(dbus.asyncCall(message));
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, watcher, [this](QDBusPendingCallWatcher *watcher) { QObject::connect(pendingCallWatcher.get(), &QDBusPendingCallWatcher::finished, pendingCallWatcher.get(), [this](QDBusPendingCallWatcher *watcher) {
if (!watcher->isError()) { if (!watcher->isError()) {
QDBusPendingReply<QVariant> reply = *watcher; QDBusPendingReply<QVariant> reply = *watcher;
if (Q_LIKELY(reply.isValid())) if (Q_LIKELY(reply.isValid()))
updateHighContrast(static_cast<Qt::ContrastPreference>(reply.value().toUInt())); updateHighContrast(static_cast<Qt::ContrastPreference>(reply.value().toUInt()));
} }
initDbus(); initDbus();
watcher->deleteLater();
}); });
} else { } else {
qCWarning(lcQpaThemeGnome) << "dbus connection failed. Last error: " << dbus.lastError(); qCWarning(lcQpaThemeGnome) << "dbus connection failed. Last error: " << dbus.lastError();

View File

@ -24,6 +24,7 @@ QT_BEGIN_NAMESPACE
class QGnomeThemePrivate; class QGnomeThemePrivate;
#if QT_CONFIG(dbus) #if QT_CONFIG(dbus)
class QDBusListener; class QDBusListener;
class QDBusPendingCallWatcher;
#endif #endif
class Q_GUI_EXPORT QGnomeTheme : public QGenericUnixTheme class Q_GUI_EXPORT QGnomeTheme : public QGenericUnixTheme
@ -66,6 +67,7 @@ public:
Qt::ContrastPreference m_contrast = Qt::ContrastPreference::NoPreference; Qt::ContrastPreference m_contrast = Qt::ContrastPreference::NoPreference;
private: private:
std::unique_ptr<QDBusListener> dbus; std::unique_ptr<QDBusListener> dbus;
std::unique_ptr<QDBusPendingCallWatcher> pendingCallWatcher;
bool initDbus(); bool initDbus();
void updateColorScheme(const QString &themeName); void updateColorScheme(const QString &themeName);
void updateHighContrast(Qt::ContrastPreference contrast); void updateHighContrast(Qt::ContrastPreference contrast);