Skip to content

fix: release each frozen display image after capture - #352

Merged
lzhgus merged 1 commit into
mainfrom
fix/release-freeze-surfaces
Oct 16, 2026
Merged

lzhgus merged 1 commit into
mainfrom
fix/release-freeze-surfaces

Conversation

@lzhgus

@lzhgus lzhgus commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Fixes #350. Thanks to @Joilence for the excellent report and measurements.

CGDisplayCreateImage follows the Create rule and returns a +1 image. We load it through dlsym and called it as @convention(c) (CGDirectDisplayID) -> CGImage?. A function pointer carries no ownership annotation, so Swift treated the result as +0 and retained it again. Every frozen display image, and its display-sized IOSurface shared with WindowServer, was never freed. The function type now returns Unmanaged<CGImage>?, taken with takeRetainedValue().

Testing

Debug build, one 3456×2234 display, vmmap --summary + heap on the Capso process after each step (3 area captures cancelled with Esc, then 2 real area captures):

after 1 2 3 4 5
main IOSurface 29.5 MB 59.0 88.5 117.9 147.6
this PR 29.5 MB 29.9 29.9 29.9 29.9

With the fix, heap shows a single live IOSurface (the latest frame) instead of one per capture. leaks --traceTree on main showed the leaked surfaces had no referencing roots, i.e. an over-retain rather than a window keeping them. Captured images are unchanged.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CEFcqadVpnqXceY8Ke7Szp


Note

Low Risk
Narrow ownership fix at the dlsym boundary for synchronous display capture; behavior unchanged aside from releasing frozen-frame memory correctly.

Overview
Fixes a memory leak in freeze-screen synchronous display capture: each CGDisplayCreateImage call via dlsym was treated as unowned (CGImage?), so Swift retained display-sized images and their IOSurface backing instead of releasing them after use.

The dynamically loaded function pointer now returns Unmanaged<CGImage>?, and syncCaptureDisplay transfers ownership with takeRetainedValue(), matching Core Graphics’ Create (+1) rule. Capture output is unchanged; repeated area/fullscreen freeze flows no longer accumulate ~30MB per cancelled or completed selection per display.

Reviewed by Cursor Bugbot for commit 70ec33a. Bugbot is set up for automated code reviews on this repo. Configure here.

CGDisplayCreateImage follows the Create rule, but the dlsym'd function
pointer returned a plain CGImage, so Swift retained it once more and every
capture leaked a display-sized IOSurface shared with WindowServer.

Fixes #350

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CEFcqadVpnqXceY8Ke7Szp
@cursor

cursor Bot commented Oct 16, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 94f5055f-5c1d-4b02-8fc4-37f97ff5767d)

@lzhgus
lzhgus merged commit f660d02 into main Oct 16, 2026
6 checks passed
@lzhgus
lzhgus deleted the fix/release-freeze-surfaces branch October 16, 2026 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Each capture leaks one IOSurface per display in WindowServer

1 participant