[leak-fix] Fix ListView.RefreshCommand memory leak (Fixes #36539) - #36659
Closed
github-actions[bot] wants to merge 1 commit into
Closed
[leak-fix] Fix ListView.RefreshCommand memory leak (Fixes #36539)#36659github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
Replace the plain ICommand.CanExecuteChanged subscription with a WeakCommandSubscription so a long-lived RefreshCommand (whose CanExecuteChanged is a plain CLR event) no longer roots the ListView and its page. Adds a Controls.Core.UnitTests regression test that fails without this fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
Open
3 tasks
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Note
🤖 AI-generated PR — produced automatically by the Memory Leak Fixer agentic workflow from issue #36539. The regression test below was observed to FAIL on unpatched
mainand PASS with this fix, on the runner. Please review carefully before merging.Fixes #36539
Refs: #36539
Target branch: main
Attempt: 2/3
The leak
ListView.RefreshCommandsubscribes to the command'sCanExecuteChangedevent (src/Controls/src/Core/ListView/ListView.cs,OnRefreshCommandChanged). The subscription was a plainnewCommand.CanExecuteChanged += OnCommandCanExecuteChangedand was only removed when the command was reassigned — never on unload. When the command'sCanExecuteChangedis a plain CLR event (as is common for custom/ViewModelICommandimplementations), the delegate target is theListView, so a shared/long-lived command strongly roots theListView(and its page). MAUI's ownCommandraisesCanExecuteChangedthrough aWeakEventManagerand would not leak, but arbitraryICommandimplementations do.The fix
Replace the plain subscription with the existing idiomatic
WeakCommandSubscription(the same helperButton/CommandElementalready use). It links theListViewto the handler via aDependentHandleso the command holds theListViewonly weakly, and disposes the previous subscription when the command is replaced. No behavior change toRefreshAllowed/CanExecuteevaluation.Regression test
ListViewMemoryLeakTests.ListViewDoesNotLeakWhenRefreshCommandIsLongLivedinsrc/Controls/tests/Core.UnitTests/ListViewMemoryLeakTests.cs. It assigns a long-livedICommand(with a plainCanExecuteChangedevent) toRefreshCommand, drops theListView, forces GC, and asserts theListViewis collected.main)All 71 neighbouring
ListViewTestsstill pass.Scope
Managed cross-platform change (
src/Controls/src/Core/ListView/ListView.cs) → all platforms. No public API change.