QDesktopUnixServices: don't leak the QDBusPendingCallWatcher

When, like in tst_QApplication::qtbug_103611(), a QApplication object
is destroyed without calling exec(), then QDesktopUnixServices leaked
its QDBusPendingCallWatcher, because the connection to the slot that
would deleteLater() the watcher was disconnected, and the watcher had
no QObject parent that would otherwise reap it.

Fix by actually deleting the watcher in ~QDesktopUnixServices(), if
it still exists (which implies disconnecting, of course).

This makes tst_QApplication asan-clean on Linux/X11 (was: "75466 bytes
leaked in 965 allocations").

Amends de609d84b9cee4a481d1718c00b09105d8c2ae69.

Pick-to: 6.8 6.5
Change-Id: I28a08abdb1be65a8746702ad304282de4e1100d7
Reviewed-by: Liang Qi <liang.qi@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit 567da5e3e0070b7a0dd9ba1acbe9347c6cc1d0fe)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-03-26 08:06:04 +01:00 committed by Qt Cherry-pick Bot
parent 8ebe07a76d
commit 81451de0e6
2 changed files with 6 additions and 3 deletions

View File

@ -397,7 +397,7 @@ QDesktopUnixServices::QDesktopUnixServices()
QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(message);
auto watcher = new QDBusPendingCallWatcher(pendingCall);
m_watcherConnection =
m_watcher = watcher;
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, watcher,
[this](QDBusPendingCallWatcher *watcher) {
watcher->deleteLater();
@ -412,7 +412,7 @@ QDesktopUnixServices::QDesktopUnixServices()
QDesktopUnixServices::~QDesktopUnixServices()
{
#if QT_CONFIG(dbus)
QObject::disconnect(m_watcherConnection);
delete m_watcher;
#endif
}

View File

@ -16,11 +16,14 @@
//
#include <qpa/qplatformservices.h>
#include <QtCore/qpointer.h>
#include <QtCore/QString>
#include <QtCore/private/qglobal_p.h>
QT_BEGIN_NAMESPACE
class QDBusPendingCallWatcher;
class QWindow;
class Q_GUI_EXPORT QDesktopUnixServices : public QPlatformServices
@ -46,7 +49,7 @@ private:
QString m_webBrowser;
QString m_documentLauncher;
#if QT_CONFIG(dbus)
QMetaObject::Connection m_watcherConnection;
QPointer<QDBusPendingCallWatcher> m_watcher = nullptr;
#endif
bool m_hasScreenshotPortalWithColorPicking = false;
};