From 5a687b084eb0ad17c836fb6ba7d25ddf44dc4856 Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Tue, 4 Oct 2022 15:10:26 +0000 Subject: [PATCH] Fix leak of D3D12 fence share handle The D3D12 fence share handle should be closed when the device is deleted. A future change will make it valid to call EndAccess after the device is destroyed, thus the handle is closed in ~Device instead of Device::DestroyImpl. It needs to live as long as ExternalImageDXGI holds a reference onto the device. Bug: chromium:1359106 Change-Id: Ib9c9aaa7fb0b5a3de035b512f8fc0316d4bd225e Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/104540 Commit-Queue: Austin Eng Reviewed-by: Corentin Wallez Reviewed-by: Sunny Sachanandani Kokoro: Kokoro --- src/dawn/native/d3d12/DeviceD3D12.cpp | 7 +++++++ src/dawn/native/d3d12/ExternalImageDXGIImpl.cpp | 2 +- src/dawn/native/d3d12/ExternalImageDXGIImpl.h | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/dawn/native/d3d12/DeviceD3D12.cpp b/src/dawn/native/d3d12/DeviceD3D12.cpp index c9a90483de..840351c104 100644 --- a/src/dawn/native/d3d12/DeviceD3D12.cpp +++ b/src/dawn/native/d3d12/DeviceD3D12.cpp @@ -193,6 +193,13 @@ MaybeError Device::Initialize(const DeviceDescriptor* descriptor) { Device::~Device() { Destroy(); + + // Close the handle here instead of in DestroyImpl. The handle is returned from + // ExternalImageDXGI, so it needs to live as long as the Device ref does, even if the device + // state is destroyed. + if (mFenceHandle != nullptr) { + ::CloseHandle(mFenceHandle); + } } ID3D12Device* Device::GetD3D12Device() const { diff --git a/src/dawn/native/d3d12/ExternalImageDXGIImpl.cpp b/src/dawn/native/d3d12/ExternalImageDXGIImpl.cpp index 268e430d95..5ffd1c76be 100644 --- a/src/dawn/native/d3d12/ExternalImageDXGIImpl.cpp +++ b/src/dawn/native/d3d12/ExternalImageDXGIImpl.cpp @@ -111,7 +111,7 @@ WGPUTexture ExternalImageDXGIImpl::BeginAccess( } } else { d3d11on12Resource = mD3D11on12ResourceCache->GetOrCreateD3D11on12Resource( - mBackendDevice, mD3D12Resource.Get()); + mBackendDevice.Get(), mD3D12Resource.Get()); if (d3d11on12Resource == nullptr) { dawn::ErrorLog() << "Unable to create 11on12 resource for external image"; return nullptr; diff --git a/src/dawn/native/d3d12/ExternalImageDXGIImpl.h b/src/dawn/native/d3d12/ExternalImageDXGIImpl.h index ee12a1664e..b3b914ca05 100644 --- a/src/dawn/native/d3d12/ExternalImageDXGIImpl.h +++ b/src/dawn/native/d3d12/ExternalImageDXGIImpl.h @@ -58,7 +58,7 @@ class ExternalImageDXGIImpl : public LinkNode { void EndAccess(WGPUTexture texture, ExternalImageDXGIFenceDescriptor* signalFence); private: - Device* mBackendDevice = nullptr; + Ref mBackendDevice; Microsoft::WRL::ComPtr mD3D12Resource; const bool mUseFenceSynchronization;