Files
ladybird/Libraries/LibCore/Platform/ScopedAutoreleasePoolMacOS.mm
Andreas Kling a25fc5ad8a LibCore+LibWeb: Add ScopedAutoreleasePool and use it on macOS
On macOS, Objective-C methods frequently return autoreleased objects
that accumulate until an autorelease pool is drained. Our event loop
(Core::EventLoop) and rendering thread both lacked autorelease pools,
causing unbounded accumulation of autoreleased objects.

The rendering thread was the worst offender: every Skia flush triggers
Metal resource allocation which sets labels on GPU textures via
-[IOGPUMetalResource setLabel:], creating autoreleased CFData objects.
With ~1M+ such objects at 112 bytes each, this leaked ~121MB. Metal
command buffer objects (_MTLCommandBufferEncoderInfo, etc.) also
accumulated, adding another ~128MB.

Add Core::ScopedAutoreleasePool, a RAII wrapper around the ObjC runtime
autorelease pool (no-op on non-macOS), and drain it:
- Every event loop pump (like NSRunLoop does)
- Every compositor loop iteration on the rendering thread
2026-03-15 11:42:43 +01:00

25 lines
467 B
Plaintext

/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/Platform/ScopedAutoreleasePool.h>
extern "C" void* objc_autoreleasePoolPush(void);
extern "C" void objc_autoreleasePoolPop(void* pool);
namespace Core {
ScopedAutoreleasePool::ScopedAutoreleasePool()
: m_pool(objc_autoreleasePoolPush())
{
}
ScopedAutoreleasePool::~ScopedAutoreleasePool()
{
objc_autoreleasePoolPop(m_pool);
}
}