Repository navigation
fix: Subscribe to Command.CanExecuteChanged weakly (backport #25965) - #26003
Merged
Merged
Conversation
A control that never enters the live tree is kept alive by a long-lived command through its CanExecuteChanged subscription. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit fc1ef84) # Conflicts: # src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_CommandCanExecuteChanged.cs
ButtonBase and MenuFlyoutItem now hold their CanExecuteChanged subscription weakly, as WinUI does (WeakEventPtr), so a long-lived command no longer keeps alive a control that never entered the live tree, e.g. an item of a MenuFlyout that is never opened. The command only references a small subscription that holds the control through a weak reference. The control keeps a handle whose finalizer removes that subscription from the command, on the UI thread. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit eff7885)
Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit e6e4535)
This comment was marked as resolved.
This comment was marked as resolved.
2 of 5 tasks
Co-Authored-By: Claude Opus 5.5 <[email protected]>
BindableButton held its ClickCommand.CanExecuteChanged subscription strongly and only removed it when detached from the window, so a long-lived command kept alive a button that was never attached. It now uses the same weak subscription as ButtonBase and MenuFlyoutItem. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
Contributor
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
Xiaoy312
approved these changes
Nov 3, 2026
Contributor
|
The build 243457 did not find any UI Test snapshots differences. |
Contributor
Author
|
This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again. |
Merged
1 task done
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.
GitHub Issue: closes #25969, closes unoplatform/kahua-private#562
Note
Backport notes for
servicing/6.8Tests/Windows_UI_Xaml_Controls/, and the cherry-pick already put the file there. The fix files are identical to fix: Subscribe to Command.CanExecuteChanged weakly #25965.BindableButton.Android.csis native-Android code that no longer exists on master. ItsClickCommand.CanExecuteChangedsubscription was strong and was only removed on detach, so a button that was never attached stayed alive. It now uses the sameWeakCanExecuteChangedSubscription. Two NativeAndroid tests cover it: a never-attached button is collected, and a live button keeps followingCanExecutethrough a GC.Uno.UIfornet10.0-androidbuilds, and the runtime tests build for Skia. The native-Android runtime tests rely on CI.PR Type:
🐞 Bugfix
What changed? 🚀
ButtonBaseandMenuFlyoutItemsubscribed toCommand.CanExecuteChangedwith a strong handler. They only removed it when the command changed or inLeaveImpl. So a control that never entered the live tree stayed alive for as long as its command, for example:MenuFlyoutthat is never opened, which in turn keep the whole screen alive throughDataContextand their parents;WinUI holds this subscription weakly (
ctl::WeakEventPtr).This PR does the same with a new internal helper,
WeakCanExecuteChangedSubscription:ICommandisn't required to be thread-safe.LeaveImpl, removes the subscription right away and skips the finalizer.AppBarButton,ToggleMenuFlyoutItem,RadioMenuFlyoutItemand the other subclasses get the fix through their base class. No otherCanExecuteChangedsubscription remains inUno.UI.Behaviour change: if a command's
removeaccessor throws, the exception is now logged at Error level. Previously it propagated from theCommandsetter or fromLeaveImpl.Not changed: subscribing only while the control is in the live tree would avoid the finalizer. However, WinUI keeps a never-loaded control's
IsEnabledin sync withCanExecute; the new tests confirm this on native WinUI. Doing so would therefore break WinUI parity.Validation
Given_CommandCanExecuteChanged: 18/18 pass. Before the fix, the leak tests fail.Uno.UIbuild, the WinAppSDK head, anddotnet format whitespaceare clean.Task.Yieldcollection loop, ignored on NativeAOT); CI will confirm.PR Checklist ✅
Screenshots Compare Test Runresults.🤖 Generated with Claude Code
This is an automatic backport of pull request #25965 done by [Mergify](https://mergify.com).