Repository navigation
fix: release each frozen display image after capture - #352
Merged
Merged
Conversation
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
Bugbot couldn't run - usage limit reachedBugbot 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #350. Thanks to @Joilence for the excellent report and measurements.
CGDisplayCreateImagefollows the Create rule and returns a +1 image. We load it throughdlsymand 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 returnsUnmanaged<CGImage>?, taken withtakeRetainedValue().Testing
Debug build, one 3456×2234 display,
vmmap --summary+heapon the Capso process after each step (3 area captures cancelled with Esc, then 2 real area captures):mainIOSurfaceWith the fix,
heapshows a single live IOSurface (the latest frame) instead of one per capture.leaks --traceTreeon 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
CGDisplayCreateImagecall viadlsymwas 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>?, andsyncCaptureDisplaytransfers ownership withtakeRetainedValue(), 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.