From 59ff87dc911840669699c0a09acb2fd7ffa012fe Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 27 Feb 2024 10:35:49 +0100 Subject: [PATCH] Windows: clean up System Tray Icon message icon The handle is not owned by the Shell, we have to clear it up ourselves. The documentation is not clear about how long the handle needs to be kept alive, so store the icon when we create it as a member of the private, and clean it up when it need to be recreated, or when the QSystemTrayIcon instance gets destroyed. Fixes: QTBUG-96348 Fixes: QTBUG-62945 Pick-to: 6.6 6.5 6.2 5.15 Change-Id: I6f93f29a415cde2cfe4e1b296295783c15b4da4b Reviewed-by: Oliver Wolff (cherry picked from commit afb74a86d8cd1ac6463fa804300480967101d7d7) Reviewed-by: Qt Cherry-pick Bot --- .../platforms/windows/qwindowssystemtrayicon.cpp | 10 +++++++++- src/plugins/platforms/windows/qwindowssystemtrayicon.h | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/plugins/platforms/windows/qwindowssystemtrayicon.cpp b/src/plugins/platforms/windows/qwindowssystemtrayicon.cpp index 6c88c5008df..ed88e250a21 100644 --- a/src/plugins/platforms/windows/qwindowssystemtrayicon.cpp +++ b/src/plugins/platforms/windows/qwindowssystemtrayicon.cpp @@ -220,11 +220,16 @@ void QWindowsSystemTrayIcon::showMessage(const QString &title, const QString &me const auto size = icon.actualSize(QSize(256, 256)); QPixmap pm = icon.pixmap(size); + if (m_hMessageIcon) { + DestroyIcon(m_hMessageIcon); + m_hMessageIcon = nullptr; + } if (pm.isNull()) { tnd.dwInfoFlags = NIIF_INFO; } else { tnd.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON; - tnd.hBalloonIcon = qt_pixmapToWinHICON(pm); + m_hMessageIcon = qt_pixmapToWinHICON(pm); + tnd.hBalloonIcon = m_hMessageIcon; } tnd.hWnd = m_hwnd; tnd.uTimeout = msecsIn <= 0 ? UINT(10000) : UINT(msecsIn); // 10s default @@ -282,7 +287,10 @@ void QWindowsSystemTrayIcon::ensureCleanup() } if (m_hIcon != nullptr) DestroyIcon(m_hIcon); + if (m_hMessageIcon != nullptr) + DestroyIcon(m_hMessageIcon); m_hIcon = nullptr; + m_hMessageIcon = nullptr; m_menu = nullptr; // externally owned m_toolTip.clear(); } diff --git a/src/plugins/platforms/windows/qwindowssystemtrayicon.h b/src/plugins/platforms/windows/qwindowssystemtrayicon.h index 2545ae91012..a50865c950f 100644 --- a/src/plugins/platforms/windows/qwindowssystemtrayicon.h +++ b/src/plugins/platforms/windows/qwindowssystemtrayicon.h @@ -55,6 +55,7 @@ private: QString m_toolTip; HWND m_hwnd = nullptr; HICON m_hIcon = nullptr; + HICON m_hMessageIcon = nullptr; mutable QPointer m_menu; bool m_ignoreNextMouseRelease = false; bool m_visible = false;