Files
ladybird/Libraries/LibCore/Platform/ScopedAutoreleasePool.h
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

40 lines
607 B
C++

/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Noncopyable.h>
#include <AK/Platform.h>
#include <LibCore/Export.h>
namespace Core {
#ifdef AK_OS_MACOS
class CORE_API ScopedAutoreleasePool {
AK_MAKE_NONCOPYABLE(ScopedAutoreleasePool);
AK_MAKE_NONMOVABLE(ScopedAutoreleasePool);
public:
ScopedAutoreleasePool();
~ScopedAutoreleasePool();
private:
void* m_pool;
};
#else
class ScopedAutoreleasePool {
public:
ScopedAutoreleasePool() = default;
~ScopedAutoreleasePool() { }
};
#endif
}