[Metal][Mac] FMetalViewport::PresentDrawLayers retains LocalDrawable and never releases it: one CAMetalDrawable leaked per presented frame

Summary

In Engine/Source/Runtime/Apple/MetalRHI/Private/MetalViewport.cpp, FMetalViewport::PresentDrawLayers (line 1001), the path without WITH_MULTI_VIEW_SLATE_WINDOW_SUPPORT (the normal Mac path) does:

CA::MetalDrawable* LocalDrawable = GetDrawable(EMetalViewportAccessDisplayLink);
LocalDrawable->retain();      // line 1024

CommittedDrawables.Add(GetCommittedDrawableToPresent(std::numeric_limits<int32>::min(), CommandBuffer, bLayersToDraw));

LocalDrawable is not used after this and is never released. The command buffer completion handler releases only the drawables in CommittedDrawables (each retained once inside GetCommittedDrawableToPresent, line 954, which is balanced), so the extra retain at line 1024 is never balanced.

Engine: 5.8.3 launcher build (CL 58210709). Suggested fix: remove the LocalDrawable = GetDrawable(…); LocalDrawable->retain(); pair at lines 1023-1024 (the drawable is already retained in GetCommittedDrawableToPresent), or release it after GetCommittedDrawableToPresent.

What Type of Bug are you experiencing?

Rendering (Graphics / Niagara)

Steps to Reproduce

  1. Package any project for Mac (Development or Shipping) and run it.
  2. Take heap PID snapshots a few minutes apart and count CAMetalDrawable instances.

Expected Result

The number of live CAMetalDrawable objects stays at the swap-chain size (about 3).

Observed Result

The live CAMetalDrawable count grows by one per frame: about 1,800 per minute at 30 fps, about 0.4 MB per minute, without limit. Seen on an M3 MacBook Air (16 GB), macOS 26.6.2, UE 5.8.3 packaged Development build.

Related report (separate leak in the same RHI): FMetalDescriptorHeap leaking its allocator on every descriptor flush, [Metal][Mac] FMetalDescriptorHeap destructor is empty: every FlushPendingDescriptorUpdates leaks an FRHIHeapDescriptorAllocator + FMetalRHIBuffer (steady memory growth on METAL_SM6)

Affects Versions

5.8

Platform(s)

Mac

Confirming this is still present in 5.8.4 (launcher CL 58902052, MetalRHI source identical to the 5.8.4-release tag). Packaged Shipping build, Apple Silicon Mac, METAL_SM6.

We applied exactly the suggested fix (removed the unmatched LocalDrawable->retain() in PresentDrawLayers) on a 5.8.4-release source build and soaked the same packaged game for 30 min:

  • Live CAMetalDrawable objects (heap PID): ~37,300 after 5 min before the fix → 2-4 for the whole run after the fix (uncapped, ~112 fps). With a 60 fps cap: 1-2.
  • RSS slope uncapped: ~6.3 MB/min → 3.4 MB/min (the rest was the separate FMetalDescriptorHeap leak).
  • No crashes, rendering unchanged.

The leak scales with frame rate, so uncapped/high-refresh users hit it hardest. Would be great to see this fixed in a 5.8 hotfix.

Thanks @parrykhai for identifying the unmatched drawable retain. We independently reproduced the same issue in a UE 5.8.4 packaged Mac Development build (SF_METAL_SM5), and the suggested removal fixed the per-frame CAMetalDrawable accumulation in our tested executable.

Environment: Mac Studio, Apple M1 Max, 64 GB unified memory, macOS 26.4.1. Normal onscreen rendering at 1809 x 1018, not NullRHI or render-offscreen.

We also traced a separate accumulation of autoreleased temporary objects through Metal submission/translation worker paths. In our allocation-stack captures, live CFData allocations came predominantly from driver UTF8String/command-encoding work reached through FMetalCommandQueue::CommitCommandBuffer and FMetalCommandEncoder::SignalEvent. These functions lacked local autorelease-pool scopes. We added MTL_SCOPED_AUTORELEASE_POOL at the start of both functions.

The minimal renderer patch against our installed 5.8.4 source is:

--- a/Engine/Source/Runtime/Apple/MetalRHI/Private/MetalViewport.cpp
+++ b/Engine/Source/Runtime/Apple/MetalRHI/Private/MetalViewport.cpp
@@ -1029,6 +1029,4 @@
 	}
 #else
-	CA::MetalDrawable* LocalDrawable = GetDrawable(EMetalViewportAccessDisplayLink);
-	LocalDrawable->retain();
 	
 	CommittedDrawables.Add(GetCommittedDrawableToPresent(std::numeric_limits<int32>::min(), CommandBuffer, bLayersToDraw));
--- a/Engine/Source/Runtime/Apple/MetalRHI/Private/MetalCommandQueue.cpp
+++ b/Engine/Source/Runtime/Apple/MetalRHI/Private/MetalCommandQueue.cpp
@@ -132,4 +132,5 @@
 void FMetalCommandQueue::CommitCommandBuffer(FMetalCommandBuffer* CommandBuffer)
 {
+	MTL_SCOPED_AUTORELEASE_POOL;
 	check(CommandBuffer);
 	INC_DWORD_STAT(STAT_MetalCommandBufferCommittedPerFrame);
--- a/Engine/Source/Runtime/Apple/MetalRHI/Private/MetalCommandEncoder.cpp
+++ b/Engine/Source/Runtime/Apple/MetalRHI/Private/MetalCommandEncoder.cpp
@@ -885,4 +885,5 @@
 void FMetalCommandEncoder::SignalEvent(MTLEventPtr Event, uint32_t SignalCount)
 {
+	MTL_SCOPED_AUTORELEASE_POOL;
 	check(CommandBuffer);
 	

We rebuilt MetalRHI from source, rebuilt the full game package, and checked that the actual packaged executable contained the repairs. Merely editing the source alongside a precompiled renderer would not validate the change.

The following sustained measurements test all three changes together:

  • Before repair: one full hour of a gameplay route, then 15 minutes at acknowledged character selection.
  • After repair: a fresh full hour of the same gameplay route, then 15 minutes at acknowledged character selection. Separate sessions had different scene populations and cache warm-up.
  • Aggregate heap snapshots used heap -s -H --noContent PID only during character selection. No forced GC or allocation-stack logging was enabled during the sustained repaired run.
Live class Before repair: early → late idle After repair: early → late idle
CAMetalDrawable 218,073 → 254,721 2 → 2
CFData 3,586,884 → 4,358,747 234 → 234
CFString 3,605,512 → 4,377,360 18,777 → 18,777

Before-repair snapshots began at approximately 32 and 656 seconds after logout; repaired snapshots began at approximately 98 and 781 seconds. Snapshot attachment can briefly perturb residency, so the chart includes those variations rather than smoothing them away.

After the cleanup window, repaired game-process physical footprint had a median of 4.084 GiB (range 4.052-4.151 GiB), ending at 4.080 GiB. The data/string/drawable class counts above were identical across the two repaired snapshots. The chart measures game-process physical footprint only, with shutdown samples excluded; overlapping resource counters and the separate managed child process are not added to it.

Seven focused regression cases exercised the actual renderer methods for drawable ownership (including delayed command-buffer completion) and Cocoa temporary-object lifetimes. Those cases failed against the original methods and passed against the repaired methods. The packaged native acceptance checks and the complete rendered gameplay/idle run also passed.

Validation scope is this packaged SM5 build and the measured 60-minute gameplay plus 15-minute idle run. Longer-duration behavior and SM6 have not been validated. The sustained results are for the combined patch; they do not separately quantify the contribution of each autorelease-pool insertion. We are sharing the drawable confirmation here, with the related worker-pool findings and minimal patch for investigation/upstream review.

Thanks @maxx_entropy for confirming this and sharing the patch! Good to know it also happens on SM5 and is still there in 5.8.4. We’re on the launcher engine, so we’ll wait for an official fix.