Skip to content

fix(audio): release COM wrappers and adopt singleton view-model lifetime#28

Merged
hoobio merged 1 commit into
mainfrom
fix/com-and-vm-lifetime-leaks
May 5, 2026
Merged

fix(audio): release COM wrappers and adopt singleton view-model lifetime#28
hoobio merged 1 commit into
mainfrom
fix/com-and-vm-lifetime-leaks

Conversation

@hoobio
Copy link
Copy Markdown
Owner

@hoobio hoobio commented May 5, 2026

Summary

Fixes a runaway thread/memory leak that grew to ~32k threads and ~4 GB private bytes over an 11-hour session. Three coupled causes, all addressed:

  • AudioSessionService.BuildSnapshot leaked an MMDevice and an AudioSessionControl per audio session per call. Both now disposed in finally blocks.
  • SessionWatcher.OnSessionCreated registered an IAudioSessionEvents sink on each new session and never unregistered it. Sinks are now tracked in a ConcurrentBag and unregistered + disposed on watcher disposal, with a _disposed guard so a late COM callback no-ops instead of registering against a dying watcher.
  • AudioEndpointService.BuildList had the same MMDevice non-disposal pattern. Same finally-block fix.
  • HostBuilderExtensions registered the three view models and their pages as Transient. Direct Frame.Content navigation never disposed them, so each page switch leaked a VM that stayed subscribed to audio events forever. Switched to Singleton: one instance per type for the app lifetime, disposed via the host on shutdown. As a side benefit, page state is preserved across navigation.

Test plan

  • Builds clean (0 warnings, 0 errors).
  • Manual: launched fresh, baseline ~108 threads / ~190 MB.
  • Manual: rapid Sessions <-> Rules switching no longer grows memory (was +160 MB in 4 minutes pre-fix; flat after fix).
  • Overnight soak: leave running 8+ hours and confirm thread count stays under ~200 and working set under ~300 MB. (Pre-fix this was the path to 32k threads.)

fix(audio): release MMDevice / AudioSessionControl wrappers and unregister event sinks
fix(app): use singleton lifetime for pages and view models so navigation does not leak VMs

Stops a runaway thread/memory leak that grew to 32k threads + 4 GB over
~11 hours of normal use. Three coupled causes, all fixed here:

- AudioSessionService.BuildSnapshot leaked an MMDevice and an
  AudioSessionControl per audio session per call. Now disposed in
  finally blocks.

- SessionWatcher.OnSessionCreated registered an IAudioSessionEvents
  sink on each new session and never unregistered it. Now tracked in
  a ConcurrentBag and unregistered + disposed on watcher disposal,
  with a _disposed guard to no-op late callbacks.

- AudioEndpointService.BuildList had the same MMDevice non-disposal
  pattern. Same finally-block fix.

- HostBuilderExtensions registered RulesViewModel, SessionsViewModel,
  SettingsViewModel and their pages as Transient. Direct Frame.Content
  navigation never disposed them, so each page switch leaked a VM
  that stayed subscribed to audio events forever. Switched to
  Singleton: one instance per type for the app lifetime, disposed
  via the host on shutdown. Page state is also preserved across
  navigation, which makes switching noticeably snappier.
@hoobio hoobio enabled auto-merge (squash) May 5, 2026 08:56
@hoobio hoobio disabled auto-merge May 5, 2026 08:57
@hoobio hoobio enabled auto-merge (squash) May 5, 2026 08:57
@hoobio hoobio merged commit 694819e into main May 5, 2026
11 checks passed
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.

1 participant