Skip to content

fix(capture): remove capture listeners so they stop piling up - #531

Merged
tieubao merged 1 commit into
developfrom
fix/memory-growth
Nov 17, 2026
Merged

tieubao merged 1 commit into
developfrom
fix/memory-growth

Conversation

@tieubao

@tieubao tieubao commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

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 --outputGraph then heap --addresses=__NSMallocBlock__: 28,204 of them share one invoke function in Hidden Bar, the Swift-to-block thunk that the disassembly shows passed only to AudioObjectAdd/RemovePropertyListenerBlock and the CoreMediaIO equivalents.
  • Hourly soak (scripts/memory-soak): malloc nodes climb from 44,675 to 93,751 over 5 hours while the user is active, flat overnight.
  • Standalone check: 50 untrack/track cycles over 42 audio processes leave 2,100 blocks with the block API, 1 with the C API.

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 silent afplay: 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.

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
@tieubao
tieubao marked this pull request as ready for review November 17, 2026 06:48
@tieubao
tieubao merged commit c0d1997 into develop Nov 17, 2026
3 checks passed
@tieubao
tieubao deleted the fix/memory-growth branch November 17, 2026 06:48
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.

High memory footprint after long uptime: heap breakdown & source code locations

1 participant