Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] iOS Shell PopAsync() causes null reference at RouteRequestBuilder #13947

Closed
paul-charlton opened this issue Mar 5, 2021 · 13 comments · Fixed by #14080
Closed

[Bug] iOS Shell PopAsync() causes null reference at RouteRequestBuilder #13947

paul-charlton opened this issue Mar 5, 2021 · 13 comments · Fixed by #14080

Comments

@paul-charlton
Copy link

Description

On iOS (simulator and device) Using Shell I have navigated to 2 subsequent using GotoAsync(). If PopAsync() is then called a NullReferenceException is thrown (see below)

Steps to Reproduce

  1. Programmatically call GotoAsync()
  2. From shown Child, then call GotoAsync() (from user input button for example)
  3. From Next Child call PopAsync() (again using user input as trigger)

Expected Behavior

No Crash

Actual Behavior

Null Reference Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at Xamarin.Forms.RouteRequestBuilder.g__AddNode|27_0 (Xamarin.Forms.BaseShellItem baseShellItem, System.String nextSegment) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Shell\RouteRequestBuilder.cs:77
at Xamarin.Forms.RouteRequestBuilder.AddMatch (Xamarin.Forms.ShellUriHandler+NodeLocation nodeLocation) [0x0003c] in D:\a\1\s\Xamarin.Forms.Core\Shell\RouteRequestBuilder.cs:70
at Xamarin.Forms.ShellUriHandler.SearchForGlobalRoutes (System.String[] segments, System.Uri startingFrom, Xamarin.Forms.ShellUriHandler+NodeLocation currentLocation, System.String[] routeKeys) [0x00042] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellUriHandler.cs:441
at Xamarin.Forms.ShellUriHandler.GenerateRoutePaths (Xamarin.Forms.Shell shell, System.Uri request, System.Uri originalRequest, System.Boolean enableRelativeShellRoutes) [0x00133] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellUriHandler.cs:288
at Xamarin.Forms.ShellUriHandler.GetNavigationRequest (Xamarin.Forms.Shell shell, System.Uri uri, System.Boolean enableRelativeShellRoutes, System.Boolean throwNavigationErrorAsException, Xamarin.Forms.ShellNavigationParameters shellNavigationParameters) [0x00018] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellUriHandler.cs:185
at Xamarin.Forms.ShellNavigationManager.GoToAsync (Xamarin.Forms.ShellNavigationParameters shellNavigationParameters) [0x000b8] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellNavigationManager.cs:44
at Xamarin.Forms.ShellSection+NavigationImpl.OnPopAsync (System.Boolean animated) [0x000e9] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellSection.cs:1070

Basic Information

  • Version with issue:
  • Last known good version: unknown 4 i think worked
  • Platform Target Frameworks:
    • iOS: 14.4
  • NuGet Packages: n/a
  • Affected Devices: iOS simulator and iPhone 7

Environment

Show/Hide Visual Studio info
Microsoft Visual Studio Enterprise 2019 (2)
Version 16.8.6
VisualStudio.16.Release/16.8.6+31019.35
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Enterprise

ADL Tools Service Provider   1.0
This package contains services used by Data Lake tools

ASA Service Provider   1.0

ASP.NET and Web Tools 2019   16.8.560.26713
ASP.NET and Web Tools 2019

ASP.NET Core Razor Language Services   16.1.0.2052803+84e121f1403378489b842e1797df2f3f5a49ac3c
Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2019   16.8.560.26713
For additional information, visit https://www.asp.net/

Automatic Versions   2.0.48
Provides tools for managing assembly versions.

Azure App Service Tools v3.0.0   16.8.560.26713
Azure App Service Tools v3.0.0

Azure Data Lake Node   1.0
This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio   2.6.3000.0
Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools   16.8.560.26713
Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio   2.6.3000.0
Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools   3.8.0-5.20604.10+9ed4b774d20940880de8df1ca8b07508aa01c8cd
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

CodeMaid   11.2.231
CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, R, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Fabric.DiagnosticEvents   1.0
Fabric Diagnostic Events

GitHub.VisualStudio   2.11.106.19330
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Material icons generator   2.0.0
This extension helps you to add material design icons to your Android, iOS or UWP project

Microsoft Azure HDInsight Azure Node   2.6.3000.0
HDInsight Node under Azure Node

Microsoft Azure Hive Query Language Service   2.6.3000.0
Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio   16.0
Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service   2.6.3000.0
Language service for Azure Stream Analytics

Microsoft Azure Stream Analytics Node   1.0
Azure Stream Analytics Node under Azure Node

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30924.1

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager   2.1.113+g422d40002e.RR
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual Studio Tools for Containers   1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Mono Debugging for Visual Studio   16.8.43 (00471f8)
Support for debugging Mono processes with Visual Studio.

NamespaceFixer   2.9
VS Extension to fix namespaces following C# and VB.NET convention

NuGet Package Manager   5.8.1
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Regex Editor   1.0
.Net Regular Expressions tools

Snapshot Debugging Extension   1.0
Snapshot Debugging Visual Studio Extension Detailed Info

SQL Server Data Tools   16.0.62102.01130
Microsoft SQL Server Data Tools

StylerPackage Extension   1.0
StylerPackage Visual Stuido Extension Detailed Info

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

TypeScript Tools   16.0.21016.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.8.0-5.20604.10+9ed4b774d20940880de8df1ca8b07508aa01c8cd
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   16.8.0-beta.20507.4+da6be68280c89131cdba2045525b80890401defd
Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions   1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

Visual Studio Tools for Kubernetes   1.0
Visual Studio Tools for Kubernetes

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

VSColorOutput   2.73
Color output for build and debug windows - https://mike-ward.net/vscoloroutput

Xamarin   16.8.000.262 (d16-8@4d60f9c)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.8.0.507 (remotes/origin/d16-8@e87b24884)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.8.112 (86385a3)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   11.1.0.26 (d16-8/a36ce73)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: 5e9cb6d
    Java.Interop: xamarin/java.interop/d16-8@79d9533
    ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000
    SQLite: xamarin/sqlite/3.32.1@1a3276b
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-8@2fb1cbc


Xamarin.iOS and Xamarin.Mac SDK   14.10.0.4 (5a05865f6)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
@paul-charlton paul-charlton added s/unverified New report that has yet to be verified t/bug 🐛 labels Mar 5, 2021
@paul-charlton
Copy link
Author

Just discovered a workaround:

                Shell.Current.Navigation.RemovePage(Shell.Current.Navigation.NavigationStack.Last());

@PureWeen
Copy link
Contributor

PureWeen commented Mar 5, 2021

@paul-charlton can you attach a small reproduction of your issue please? I'll need a bit more information about your routes before I can debug this

@holecekp
Copy link

holecekp commented Mar 19, 2021

The same problem is very likely also on Android. I have just received a crash report from AppCenter for my Android app and the error message and the stack trace is almost identical with the ones reported here. However, I am not able to reproduce it locally and I do not know which steps the user did so that I could reproduce it.

The application opens a page by calling await Shell.Current.Navigation.PushModalAsync(designCustomLevel);. The crash is reported for the situation, when the user returned from the page by a method that calls await Shell.Current.Navigation.PopAsync();.

The error is System.NullReferenceException: Object reference not set to an instance of an object.
Stack trace for the Android app:
xamarin NRE

Android version: 7.0

Update: I was able to reproduce this crash also locally in Android 10 emulator. It happens randomly. Most of the times, it works well, but sometimes the same code causes crash. The stack trace is slightly different this time:

[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
[mono-rt]   at Xamarin.Forms.RouteRequestBuilder.<AddMatch>g__AddNode|27_0 (Xamarin.Forms.BaseShellItem baseShellItem, System.String nextSegment) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Shell\RouteRequestBuilder.cs:77 
[mono-rt]   at Xamarin.Forms.RouteRequestBuilder.AddMatch (Xamarin.Forms.ShellUriHandler+NodeLocation nodeLocation) [0x0001e] in D:\a\1\s\Xamarin.Forms.Core\Shell\RouteRequestBuilder.cs:67 
[mono-rt]   at Xamarin.Forms.ShellUriHandler.GenerateRoutePaths (Xa03-20 20:20:29.724 E/mono-rt (27634):   at Xamarin.Forms.ShellUriHandler.SearchForGlobalRoutes (System.String[] segments, System.Uri startingFrom, Xamarin.Forms.ShellUriHandler+NodeLocation currentLocation, System.String[] routeKeys) [0x00042] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellUriHandler.cs:441 
[mono-rt]   at Xamarin.Forms.ShellUriHandler.GenerateRoutePaths (Xamarin.Forms.Shell shell, System.Uri request, System.Uri originalRequest, System.Boolean enableRelativeShellRoutes) [0x00133] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellUriHandler.cs:288 
[mono-rt]   at Xamarin.Forms.ShellUriHandler.GetNavigationRequest (Xamarin.Forms.Shell shell, System.Uri uri, System.Boolean enableRelativeShellRoutes, System.Boolean throwNavigationErrorAsException, Xamarin.Forms.ShellNavigationParameters shellNavigationParameters) [0x00018] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellUriHandler.cs:185 
[mono-rt]   at Xamarin.Forms.ShellNavigationManager.GoToAsync (Xamarin.Forms.ShellNavigationParameters shellNavigationParameters) [0x000b8] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellNavigationManager.cs:44 
[mono-rt]   at Xamarin.Forms.ShellSection+NavigationImpl.OnPopAsync (System.Boolean animated) [0x000e9] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellSection.cs:1070 
[mono-rt]   at MorseCode.Views.Learning.DesignCustomLevel.ConfirmChoice (System.Object sender, System.EventArgs e) [0x0007f] in D:\dotNET\MorseCode\MorseCode\MorseCode\Views\Learning\DesignCustomLevel.xaml.cs:55 
[mono-rt]   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.33(intptr,intptr)
[mono-rt]   at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.33(intptr,intptr)

@jsuarezruiz
Copy link
Contributor

@holecekp Could you attach a sample where reproduce the issue?

@jsuarezruiz jsuarezruiz added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Mar 23, 2021
@holecekp
Copy link

holecekp commented Mar 23, 2021

I have figured out when the crash happens. I have a button that opens a new page (await Shell.Current.Navigation.PushModalAsync(...)). The page however takes some time before it is rendered. Meanwhile, if the user manages to tap again on the button that calls PushModalAsync, the page opens all right, but the subsequent returning by PopAsync crashes the application.

So the workaround is just to prevent multiple taps on the button:

        bool pageIsOpening = false;
        private async void OpenPage(object sender, System.EventArgs e)
        {
            // Prevent clicking on the button multiple times as it crashes the app when the user returns back.
            // https://github.com/xamarin/Xamarin.Forms/issues/13947
            if (pageIsOpening)
                return;

            pageIsOpening = true;
            var myPage = new MyPage();
            await Shell.Current.Navigation.PushModalAsync(myPage);
            pageIsOpening = false;
        }

I am not sure if this is a bug as calling PushModalAsync multiple times before the first call was handled is not very standard situation. @jsuarezruiz: Should I still create a repro project for this?

Maybe, a more specific error message would be helpful in these cases instead of NRE on PopAsync.

@jsuarezruiz
Copy link
Contributor

@holecekp Thanks for the details. @PureWeen what do you think?

@paul-charlton
Copy link
Author

Apologies for the delay I've been trying to create a sample project without success. My issue is different to @holecekp in that this doesn't seem to be related to a long running rendering process,

it's very much a push two additional pages then the pop fails.

However, as mentioned I can't recreate in a sample project which means I'm more than likely doing something a bit odd somewhere.

I'm using
Routing.RegisterRoute("routename", typeof(Page))
to register the routes and
Shell.Current.GotoAsync("routename")
to navigate there

I'd be massively grateful if anyone has any suggestions as to where to look to resolve this!!

@Redth Redth removed the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Mar 30, 2021
@PureWeen
Copy link
Contributor

@paul-charlton @holecekp

This might fix your issue
#14080

Instead of using Navigation.(Push/Pop)Modal can you try to use GoToAsync https://devblogs.microsoft.com/xamarin/xamarin-forms-shell-quick-tip-modal-navigation/

and see if that fixes your exceptions?

@PureWeen PureWeen removed the s/unverified New report that has yet to be verified label Mar 30, 2021
@paul-charlton
Copy link
Author

@PureWeen - gave that a try but it just doesn't do anything. No crashing though, but no navigation either.

This works on the level lower where the error didn't occur.

@paul-charlton
Copy link
Author

paul-charlton commented Mar 31, 2021

I've got a reproduction sample (https://github.com/paul-charlton/Bug_XamarinShellPopNavigation). To replicate - hit goto shell, then start, then push, push, push, pop.

It seems to happen based on how the Shell.xaml has been laid out (the nested routes seem to be the issue). I've changed the startup project to this:

    <TabBar Route="home">

        <Tab
            Title="Main"
            Icon="icon_about.png"
            Route="default">

            <ShellContent
                Title="About"
                ContentTemplate="{DataTemplate local:AboutPage}"
                Route="AboutPage" />

        </Tab>

        <Tab
            Title="Items"
            Icon="icon_feed.png"
            Route="default">

            <ShellContent Title="Browse" ContentTemplate="{DataTemplate local:ItemsPage}" />

        </Tab>
    </TabBar>

@PureWeen
Copy link
Contributor

PureWeen commented Apr 2, 2021

@paul-charlton I tested with the PR I linked to the issue here and it seems to fix your issue

You can test the nuget here
https://dev.azure.com/xamarin/public/_build/results?buildId=38180&view=artifacts&pathAsName=false&type=publishedArtifacts

@holecekp
Copy link

holecekp commented May 7, 2021

I have written in #13947 (comment) that the problem was solved in my case by preventing double tapping on a button that triggers the navigation. Now it seems that even though it helped a lot, this is not a perfect solution. I have received a report that my application still crashes occasionally on Android even after preventing double tapping.

@holecekp
Copy link

holecekp commented Nov 26, 2021

The issue is still there (XF 5.0 SR 5) but the type of exception changed. Instead of NRE, I am getting System.ArgumentException: unable to figure out route for: //learn/D_FAULT_Tab7/D_FAULT_ShellContent6/D_FAULT_DesignCustomLevel22 Parameter name: uri ".

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants