From d5dfa3ef5e2f603db4fc2388a29719839217a62d Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Fri, 30 May 2025 00:07:25 +0900 Subject: [PATCH] d3d12screencapturesrc: Fix desktop handle leak Calling CloseDesktop() on a handle that is currently in use will fail. Close the handle after current desktop handle change Part-of: --- .../gst-plugins-bad/sys/d3d12/gstd3d12dxgicapture.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/subprojects/gst-plugins-bad/sys/d3d12/gstd3d12dxgicapture.cpp b/subprojects/gst-plugins-bad/sys/d3d12/gstd3d12dxgicapture.cpp index 1ecf94acf0..fa9ad5ea9c 100644 --- a/subprojects/gst-plugins-bad/sys/d3d12/gstd3d12dxgicapture.cpp +++ b/subprojects/gst-plugins-bad/sys/d3d12/gstd3d12dxgicapture.cpp @@ -429,13 +429,13 @@ public: } } - HDESK hdesk = OpenInputDesktop (0, FALSE, GENERIC_ALL); + auto prev_hdesk = GetThreadDesktop (GetCurrentThreadId ()); + auto hdesk = OpenInputDesktop (0, FALSE, GENERIC_ALL); if (hdesk) { if (!SetThreadDesktop (hdesk)) { GST_WARNING ("SetThreadDesktop() failed, error %lu", GetLastError()); } - CloseDesktop (hdesk); } else { GST_WARNING ("OpenInputDesktop() failed, error %lu", GetLastError()); } @@ -469,6 +469,12 @@ public: if (FAILED (hr)) hr = output1->DuplicateOutput(device, &dupl_); + if (prev_hdesk) + SetThreadDesktop (prev_hdesk); + + if (hdesk) + CloseDesktop (hdesk); + if (FAILED (hr)) { if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE) { GST_ERROR ("Hit the max allowed number of Desktop Duplication session");