Repository navigation
[shell] [windows] fix NRE when clearing shell items - #15220
Merged
Merged
Conversation
Fixes: dotnet#13775 You can cause MAUI to throw an NRE by doing: 1. Debug/run, have XAML hot reload enabled 2. Comment out the `ShellContent` in `AppShell.xaml`, hot reload 3. Uncomment the `ShellContent` in `AppShell.xaml`, hot reload Crashes with: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Maui.Controls.Handlers.ShellSectionHandler.SetVirtualView(IElement view) at Microsoft.Maui.Controls.Handlers.ShellItemHandler.UpdateCurrentItem() at Microsoft.Maui.Controls.Handlers.ShellItemHandler.MapCurrentItem(ShellItemHandler handler, ShellItem item) at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0.<Add>b__0(IElementHandler h, IElement v) at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) at Microsoft.Maui.Controls.Handlers.ShellItemHandler.SetVirtualView(IElement view) at Microsoft.Maui.Controls.Platform.ShellView.CreateShellItemView() at Microsoft.Maui.Controls.Platform.ShellView.SwitchShellItem(ShellItem newItem, Boolean animate) at Microsoft.Maui.Controls.Handlers.ShellHandler.MapCurrentItem(ShellHandler handler, Shell view) at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0.<Add>b__0(IElementHandler h, IElement v) at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) at Microsoft.Maui.PropertyMapper.UpdateProperty(IElementHandler viewHandler, IElement virtualView, String property) at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(String property) at Microsoft.Maui.Controls.Element.OnPropertyChanged(String propertyName) at Microsoft.Maui.Controls.Shell.OnPropertyChanged(String propertyName) at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes) at Microsoft.Maui.Controls.Element.SetValueFromRenderer(BindableProperty property, Object value) at Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(ShellNavigationParameters shellNavigationParameters, ShellNavigationRequest navigationRequest) at Microsoft.Maui.Controls.Shell.<Initialize>g__SetCurrentItem|172_1() at Microsoft.Maui.Controls.Shell.<Initialize>b__172_0(Object s, NotifyCollectionChangedEventArgs e) at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0() I could reproduce this in a test by simply doing: shell.CurrentItem = new ShellContent { Content = page }; shell.Items.Clear(); shell.CurrentItem = new ShellContent { Content = page }; I added appropriate null checking and the problem appears to be solved now.
jonathanpeppers
marked this pull request as ready for review
May 22, 2023 19:02
Member
Author
|
If this PR is merged before this one: I can update #15098 to fix the merge conflicts. |
PureWeen
requested changes
May 22, 2023
| ((IShellController)_shellSection.FindParentOfType<Shell>()!).RemoveAppearanceObserver(this); | ||
|
|
||
| var shell = _shellSection.FindParentOfType<Shell>() as IShellController; | ||
| shell?.RemoveAppearanceObserver(this); |
Member
There was a problem hiding this comment.
I think we need to store shell locally when the observer is set on line 75 that way we can successfully remove (this) from the list of AppearanceObservers. Otherwise, this Handler will probably remain inside the list of appearance observers inside shell.
maui/src/Controls/src/Core/Shell/Shell.cs
Line 313 in f6cf7c7
Or trigger this tear down code via
Member
Author
There was a problem hiding this comment.
It seems like this is a second problem.
I updated the test to check for this, and then fixed it. But if this fails on iOS/Android it's a problem in other handlers I didn't touch?
PureWeen
approved these changes
May 22, 2023
PureWeen
enabled auto-merge (squash)
May 22, 2023 20:42
rmarinho
pushed a commit
that referenced
this pull request
May 30, 2023
* [shell] [windows] fix NRE when clearing shell items Fixes: #13775 You can cause MAUI to throw an NRE by doing: 1. Debug/run, have XAML hot reload enabled 2. Comment out the `ShellContent` in `AppShell.xaml`, hot reload 3. Uncomment the `ShellContent` in `AppShell.xaml`, hot reload Crashes with: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Maui.Controls.Handlers.ShellSectionHandler.SetVirtualView(IElement view) at Microsoft.Maui.Controls.Handlers.ShellItemHandler.UpdateCurrentItem() at Microsoft.Maui.Controls.Handlers.ShellItemHandler.MapCurrentItem(ShellItemHandler handler, ShellItem item) at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0.<Add>b__0(IElementHandler h, IElement v) at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) at Microsoft.Maui.Controls.Handlers.ShellItemHandler.SetVirtualView(IElement view) at Microsoft.Maui.Controls.Platform.ShellView.CreateShellItemView() at Microsoft.Maui.Controls.Platform.ShellView.SwitchShellItem(ShellItem newItem, Boolean animate) at Microsoft.Maui.Controls.Handlers.ShellHandler.MapCurrentItem(ShellHandler handler, Shell view) at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0.<Add>b__0(IElementHandler h, IElement v) at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) at Microsoft.Maui.PropertyMapper.UpdateProperty(IElementHandler viewHandler, IElement virtualView, String property) at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(String property) at Microsoft.Maui.Controls.Element.OnPropertyChanged(String propertyName) at Microsoft.Maui.Controls.Shell.OnPropertyChanged(String propertyName) at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes) at Microsoft.Maui.Controls.Element.SetValueFromRenderer(BindableProperty property, Object value) at Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(ShellNavigationParameters shellNavigationParameters, ShellNavigationRequest navigationRequest) at Microsoft.Maui.Controls.Shell.<Initialize>g__SetCurrentItem|172_1() at Microsoft.Maui.Controls.Shell.<Initialize>b__172_0(Object s, NotifyCollectionChangedEventArgs e) at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0() I could reproduce this in a test by simply doing: shell.CurrentItem = new ShellContent { Content = page }; shell.Items.Clear(); shell.CurrentItem = new ShellContent { Content = page }; I added appropriate null checking and the problem appears to be solved now. * Update test, call RemoveAppearanceObserver more reliably
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.
Fixes: #13775
You can cause MAUI to throw an NRE by doing:
Debug/run, have XAML hot reload enabled
Comment out the
ShellContentinAppShell.xaml, hot reloadUncomment the
ShellContentinAppShell.xaml, hot reloadCrashes with:
I could reproduce this in a test by simply doing:
I added appropriate null checking and the problem appears to be solved now.