You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Skia] ButtonBase and MenuFlyoutItem: a long-lived Command keeps never-loaded controls alive through CanExecuteChangedΒ #25969
ButtonBase (Button, AppBarButton, β¦) and MenuFlyoutItem (ToggleMenuFlyoutItem, RadioMenuFlyoutItem, β¦) subscribe to Command.CanExecuteChanged with a strong handler. The handler is only removed when Command changes or when the control leaves the live tree.
A control that never enters the live tree never removes it, so a command that lives longer than the control keeps the control alive. A view model's command is a typical example. Common cases:
The MenuFlyoutItems of a MenuFlyout that is never opened. They also keep the whole page alive, through their DataContext and parents, even after the page is shown and closed.
A Button with a Command that is created but never shown.
Expected behavior π―
A command does not keep its controls alive, as on WinUI, which holds this subscription weakly (WeakEventPtr). A control that is still alive keeps updating IsEnabled from CanExecute, loaded or not.
How to reproduce it (as minimally and precisely as possible) π¬
varcommand=newMyCommand();// long-lived, e.g. on an app-wide view modelvarrefs=newList<WeakReference>();for(vari=0;i<100;i++){varmenu=newMenuFlyout();menu.Items.Add(newMenuFlyoutItem{Text="Delete",Command=command});varpage=newGrid{Children={newButton{Content="...",Flyout=menu}}};refs.Add(newWeakReference(page));// never shown, or shown then removed}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();// Expected 0, actual 100: every page is rooted by command.CanExecuteChangedConsole.WriteLine(refs.Count(r =>r.IsAlive));
Workaround π οΈ
Clear Command on the items before dropping the page. Or use an ICommand whose CanExecuteChanged holds its handlers weakly.
Affected platforms π±π»π₯οΈ
All platforms π
Uno.Sdk version (and other relevant versions) π¦
Reproduced on Uno.Sdk 6.7.30 and 7.0.0-dev.130.
IDE version π§βπ»
No response
Anything else we need to know? π¬
Not a regression from a recent change. The subscription has been strong since these controls were ported.
Activity
test: Link the CanExecuteChanged leak tests to #25969
test: Link the CanExecuteChanged leak tests to unoplatform#25969