Repository navigation
fix(capture): remove capture listeners so they stop piling up - #531
Merged
Merged
Conversation
CaptureActivityMonitor registered its CoreAudio and CoreMediaIO listeners with the block API. Swift wraps a closure in a new block on every call, so each remove passed a different block than its add and matched nothing: every listener stayed registered for the life of the app, one per audio process and camera on each device change. After 5 hours the installed 1.13.0 held 28,204 such blocks. Use the C function API, which removes by proc and client data. The client data is an id looked up on the main queue, so a late callback after deinit finds no monitor. A test counts live blocks with heap across 100 monitor cycles: 4,681 left before, 0 after. Fixes #386
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 #386 (and #326, folded into it).
The leak
CaptureActivityMonitor(macOS 27 capture detection) registered its CoreAudio and CoreMediaIO property listeners with the block API and removed them on every device change. Swift wraps a closure in a new block on every call, so the remove never matched the add: every listener stayed registered for the life of the app, one per audio process and camera per device change. More duplicate listeners also meant more callbacks, so the rate grew with uptime.Evidence, from the installed 1.13.0 (21) on a Mac mini, 5 hours of uptime:
heap: 29,119 live__NSMallocBlock__(48 bytes each), the reporter's growing type.leaks --outputGraphthenheap --addresses=__NSMallocBlock__: 28,204 of them share one invoke function in Hidden Bar, the Swift-to-block thunk that the disassembly shows passed only toAudioObjectAdd/RemovePropertyListenerBlockand the CoreMediaIO equivalents.scripts/memory-soak): malloc nodes climb from 44,675 to 93,751 over 5 hours while the user is active, flat overnight.The fix
Use the C function API (
AudioObjectAddPropertyListener,CMIOObjectAddPropertyListener), which removes by proc and client data. The client data is an integer id looked up on the main queue, so a callback in flight after deinit finds no monitor. Change callbacks still arrive on the main queue (checked with a silentafplay: 4 calls, all on main).Tests
New
CaptureActivityMonitorTests.testRemovedListenersDoNotStayRegistered: 100 monitor create and destroy cycles, live block count read with/usr/bin/heap. Before: 4,681 blocks left. After: 0. Full suite run in the mac-ui VM.Not proven: the reporter's other suspects (hover monitor, auto-hide timer, images, menu observers) were checked earlier and do not show in the heap; the soak after this ships is the end-to-end check.