[WGC] don't leak resources when capture session ends

To free resources related with the capture session, users must `Close`
the frame pool, otherwise WGC's internal worker thread never exits, and
references to some WinRT objects are never released and so those objects
also leak.

Also, construction of the frame pool may fail, and since it is
constructed after `WgcCaptureSession` has been constructed (as part of
`WgcCaptureSession::StartCapture`, the destructor will be invoked and
call `WgcCaptureSession::RemoveFrameArrivedEventHandler`, which will
`DCHECK` in debug builds and crash. In release builds this isn't an
issue since `frame_arrived_token_` being not null also guarantees that
`frame_pool_` is not null.

Bug: chromium:482343989
Change-Id: I790b2f4fd1399bbf9b4e9fd0e93977103efabab5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/447560
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Alexander Cooper <alcooper@chromium.org>
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47043}
This commit is contained in:
Yuval Atia 2026-02-18 13:06:25 +02:00 committed by WebRTC LUCI CQ
parent 6f9db9ffc6
commit 7de2447ebd
2 changed files with 17 additions and 1 deletions

View File

@ -189,6 +189,7 @@ Hopin Ltd. <*@hopin.to>
HyperConnect Inc. <*@hpcnt.com>
Igalia S.L. <*@igalia.com>
Intel Corporation <*@intel.com>
Island Technology, Inc. <*@island.io>
LG Electronics, Inc. <*@lge.com>
Life On Air Inc. <*@lifeonair.com>
LiveKit <*@livekit.io>

View File

@ -39,6 +39,7 @@
using Microsoft::WRL::ComPtr;
namespace WGC = ABI::Windows::Graphics::Capture;
using IClosable = ABI::Windows::Foundation::IClosable;
namespace webrtc {
namespace {
@ -166,6 +167,18 @@ WgcCaptureSession::WgcCaptureSession(intptr_t source_id,
WgcCaptureSession::~WgcCaptureSession() {
RemoveEventHandlers();
if (frame_pool_) {
ComPtr<IClosable> closable;
HRESULT hr = frame_pool_.As(&closable);
if (FAILED(hr)) {
RTC_LOG(LS_WARNING) << "Failed to query frame pool as IClosable: " << hr;
return;
}
hr = closable->Close();
if (FAILED(hr)) {
RTC_LOG(LS_WARNING) << "Failed to close frame pool: " << hr;
}
}
}
HRESULT WgcCaptureSession::StartCapture(const DesktopCaptureOptions& options) {
@ -748,7 +761,9 @@ HRESULT WgcCaptureSession::OnItemClosed(WGC::IGraphicsCaptureItem* sender,
void WgcCaptureSession::RemoveEventHandlers() {
RemoveItemClosedEventHandler();
RemoveFrameArrivedEventHandler();
if (frame_pool_) {
RemoveFrameArrivedEventHandler();
}
}
void WgcCaptureSession::RemoveItemClosedEventHandler() {