From 4e07ca0dfe9c046cc8c75a95f41be0351acfd056 Mon Sep 17 00:00:00 2001 From: "Bernhart, Bryan" Date: Mon, 12 Dec 2022 09:24:03 -0800 Subject: [PATCH] Release leaked D3D allocations during testing. Fixes bug where CRT leak checker re-reports purposely leaked D3D allocations. --- .../d3d12/DebugResourceAllocatorD3D12.cpp | 10 ++++++++++ src/gpgmm/d3d12/DebugResourceAllocatorD3D12.h | 20 ++++--------------- src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp | 5 ++++- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/gpgmm/d3d12/DebugResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/DebugResourceAllocatorD3D12.cpp index df1602d1d..7b45b373a 100644 --- a/src/gpgmm/d3d12/DebugResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/DebugResourceAllocatorD3D12.cpp @@ -55,6 +55,16 @@ namespace gpgmm::d3d12 { } } + void DebugResourceAllocator::ReleaseLiveAllocationsForTesting() { + std::lock_guard lock(mMutex); + for (auto allocationEntry : mLiveAllocations) { + allocationEntry->GetValue().GetAllocator()->DeallocateMemory( + std::unique_ptr(allocationEntry->GetValue().GetAllocation())); + } + + mLiveAllocations.clear(); + } + void DebugResourceAllocator::AddLiveAllocation(ResourceAllocation* allocation) { std::lock_guard lock(mMutex); diff --git a/src/gpgmm/d3d12/DebugResourceAllocatorD3D12.h b/src/gpgmm/d3d12/DebugResourceAllocatorD3D12.h index e20ba03f4..070c5068b 100644 --- a/src/gpgmm/d3d12/DebugResourceAllocatorD3D12.h +++ b/src/gpgmm/d3d12/DebugResourceAllocatorD3D12.h @@ -22,28 +22,16 @@ namespace gpgmm::d3d12 { class ResourceAllocation; - /** \brief DebugResourceAllocator tracks "live" allocations so they can be reported if leaked. - - A "live" allocation means the allocation was created (allocated) but not released - (de-allocated). - - Use `gpgmm_enable_allocator_leak_checks = true` to always report for leaks. - */ + // DebugResourceAllocator tracks "live" allocations so they can be reported if leaked. + // A "live" allocation means the allocation was created (allocated) but not released + // (de-allocated). class DebugResourceAllocator final : public MemoryAllocator { public: DebugResourceAllocator() = default; - /** \brief Add a "live" allocation. - - @param allocation A pointer to a ResourceAllocation to track. - */ void AddLiveAllocation(ResourceAllocation* allocation); - - /** \brief Report "live" allocations. - - Dumps outstanding or "live" allocations. - */ void ReportLiveAllocations() const; + void ReleaseLiveAllocationsForTesting(); private: void DeallocateMemory(std::unique_ptr allocation) override; diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index f8494b676..4dd9fb86e 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -780,9 +780,12 @@ namespace gpgmm::d3d12 { ResourceAllocator::~ResourceAllocator() { GPGMM_TRACE_EVENT_OBJECT_DESTROY(this); - // Give the debug allocator the first chance to report leaks. + // Give the debug allocator the first chance to report allocation leaks. + // If allocation leak exists, report then release them immediately to prevent another leak + // check from re-reporting the leaked allocation. if (mDebugAllocator) { mDebugAllocator->ReportLiveAllocations(); + mDebugAllocator->ReleaseLiveAllocationsForTesting(); } // Destroy allocators in the reverse order they were created so we can record delete events