Xamarin.Forms
                                
                                 Xamarin.Forms copied to clipboard
                                
                                    Xamarin.Forms copied to clipboard
                            
                            
                            
                        [Bug] App crash when click menu icon in MasterDetailPage
Description
It happens randomly and I got 3 crashes in last month. It happens when I click menu icon in MasterDetailPage.
After looking into the Source code, the crash happened at:
https://github.com/xamarin/Xamarin.Forms/blob/main/Xamarin.Forms.Core/NavigationPage.cs#L149
public static bool GetHasBackButton(Page page)
{
  if (page == null)
  throw new ArgumentNullException("page");
  return (bool)page.GetValue(HasBackButtonProperty);
}
Here is the StackTrace:
SIGABRT: Value cannot be null. Parameter name: page
Stack traces
NavigationPage.GetHasBackButton (Xamarin.Forms.Page page) D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:149 NavigationRenderer+ParentingViewController.UpdateLeftBarButtonItem (Xamarin.Forms.Page pageBeingRemoved) D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:1246 <.ctor>b__14_0 (Xamarin.Forms.Platform.iOS.IVisualElementRenderer sender) D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:44 (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&) RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:409 MethodBase.Invoke (System.Object obj, System.Object[] parameters) /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Reflection/MethodBase.cs:53 MessagingCenter+Subscription.InvokeCallback (System.Object sender, System.Object args) D:\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:94 MessagingCenter.InnerSend (System.String message, System.Type senderType, System.Type argType, System.Object sender, System.Object args) D:\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:217 IMessagingCenter.Send[TSender] (TSender sender, System.String message) MessagingCenter.Send[TSender] (TSender sender, System.String message) TabletFlyoutPageRenderer.HandlePropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\TabletFlyoutPageRenderer.cs:471 (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs) BindableObject.OnPropertyChanged (System.String propertyName) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:266 Element.OnPropertyChanged (System.String propertyName) D:\a\1\s\Xamarin.Forms.Core\Element.cs:362 MasterDetailPage.OnPropertyChanged (System.String propertyName) D:\a\1\s\Xamarin.Forms.Core\FlyoutPage.cs:292 BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:510 BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446 BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374 BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349 FlyoutPage.set_IsPresented (System.Boolean value) D:\a\1\s\Xamarin.Forms.Core\FlyoutPage.cs:62 NavigationRenderer+<>c__DisplayClass79_0.<SetFlyoutLeftBarButton>g__OnItemTapped|1 (System.Object sender, System.EventArgs e) D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:868 UIBarButtonItem+Callback.Call (Foundation.NSObject sender) /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIBarButtonItem.cs:30 (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:86 UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:65 Application.Main (System.String[] args)
Any solution?
Steps to Reproduce
Expected Behavior
Actual Behavior
App crashed.
Basic Information
- Version with issue: Xamarin.forms 5.0.02012
- Last known good version:
- Platform Target Frameworks: 
- iOS:
- Android:
- UWP:
 
- Android Support Library / AndroidX Version:
- NuGet Packages:
- Affected Devices:
Environment
Show/Hide Visual Studio info
Build Logs
Screenshots
Reproduction Link
Workaround
@XfHua Based on that piece of code seems like page is null when trying to navigate to a menu item on iOS. Could you share more information?. How many menu items are in the Flyout?, could you reproduce it or have more details or some steps (like tapping menu items quickly etc)?
No, I can't reproduce it and it only happened 3 times in last month. I have 9 menu items in the flyout and I have a custom renderer of navigationpage:
public class ExtendedNavigatePageRenderer : NavigationRenderer { UIImage titleImage;
    public ExtendedNavigatePageRenderer()
    {
        titleImage = UIImage.FromBundle("title_bar_unit.png").CreateResizableImage(UIEdgeInsets.Zero, UIImageResizingMode.Tile); 
    }
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        var height = NavigationBar.Bounds.Height;
        CommonSettings.CommonSettings.NavigationBarHeight = height;
    }
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        this.NavigationBar.SetBackgroundImage(titleImage, UIBarMetrics.Default);
    }
    public override void PushViewController(UIViewController viewController, bool animated)
    {
        base.PushViewController(viewController, animated);
        try
        {
            var currentPage = (this.Element as Xamarin.Forms.NavigationPage)?.CurrentPage;
            if (this.NavigationBar == null || currentPage == null)
                return;
            var buttonItems = TopViewController.NavigationItem.RightBarButtonItems;
            foreach (var button in buttonItems)
            {
                if (button != null && button.Image != null)
                {
                    button.Image = button.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
                    button.TintColor = UIColor.Clear;
                }
            }
        }
        catch (Exception e)
        {
        }
    }
}
@jsuarezruiz I did not tap menu items quickly.
We also receive this crash occasionally:
System.ArgumentNullException
Message: Value cannot be null. Parameter name: page
Xamarin.Forms.NavigationPage.GetHasBackButton(Xamarin.Forms.Page page) in <b1baaea67aeb409ca7e75b039f7cca37#28c4f45704bf98bb1cf9c356977a2088>
Xamarin.Forms.Platform.iOS.NavigationRenderer+ParentingViewController.UpdateLeftBarButtonItem(Xamarin.Forms.Page pageBeingRemoved) in <0648e2dffe9e4201b8c6e274ced6579f#28c4f45704bf98bb1cf9c356977a2088>
Xamarin.Forms.Platform.iOS.NavigationRenderer..ctor(Xamarin.Forms.Platform.iOS.IVisualElementRenderer sender) in <0648e2dffe9e4201b8c6e274ced6579f#28c4f45704bf98bb1cf9c356977a2088>
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
System.Reflection.MonoMethod.Invoke(System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) in <2b439461a53d406abf16d6e5e34ece7f#28c4f45704bf98bb1cf9c356977a2088>
Hi. We are experiencing this issue consistently on an iPad simulator. Is there any information we have for a fix? @jsuarezruiz
Same here
also seeing this crash nearly daily on our userbase. @DanielGlick you said you could experience this consistently - does that mean you've reproduced the issue? can you share more details?
@MitchBomcanhao Its been a long time since I looked at this. I believe we implemented a workaround for this but I cant remember