fix(audio): release COM wrappers and adopt singleton view-model lifetime#28
Merged
Conversation
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.
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.
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.BuildSnapshotleaked anMMDeviceand anAudioSessionControlper audio session per call. Both now disposed infinallyblocks.SessionWatcher.OnSessionCreatedregistered anIAudioSessionEventssink on each new session and never unregistered it. Sinks are now tracked in aConcurrentBagand unregistered + disposed on watcher disposal, with a_disposedguard so a late COM callback no-ops instead of registering against a dying watcher.AudioEndpointService.BuildListhad the sameMMDevicenon-disposal pattern. Samefinally-block fix.HostBuilderExtensionsregistered the three view models and their pages asTransient. DirectFrame.Contentnavigation never disposed them, so each page switch leaked a VM that stayed subscribed to audio events forever. Switched toSingleton: 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
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