-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fix: Prevent memory leak in view model lifecycle #40216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a memory leak by disposing previous PageViewModel
instances when CurrentPage
is updated in ShellViewModel
.
- Converted
CurrentPage
from an auto-generated observable property to a manual property with disposal logic - Removed the
[ObservableProperty]
attribute onCurrentPage
- Added a backing field
_currentPage
and custom setter to dispose old pages
Comments suppressed due to low confidence (2)
src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ShellViewModel.cs:34
- [nitpick] Add XML documentation to the
CurrentPage
property to note that assigning a new page will dispose of the previous instance.
public PageViewModel CurrentPage
src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ShellViewModel.cs:42
- Consider adding a unit test to verify that when
CurrentPage
is updated, the previousPageViewModel
is properly disposed.
if (oldValue is IDisposable disposable)
src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ShellViewModel.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Summary of the Pull Request
A memory leak was identified where activating a Command Palette extension via a hotkey would lead to a linear increase in resource consumption and a corresponding decrease in search performance.
Each activation created a new
PageViewModel
instance for the extension's UI. However, theShellViewModel
did not dispose of the previousPageViewModel
instance when a new one was created.The fix is implemented in
Microsoft.CmdPal.UI.ViewModels/ShellViewModel.cs
by ensuring thatPageViewModel
instances are correctly disposed of when they are no longer active.PR Checklist
Validation Steps Performed
GetItems
is invoked.Before the Fix (PowerToys v0.91.1)
GetItems
12 times.2025-06-25.00-04-55.mp4
After the Fix
GetItems
only once.after.fix.mp4