Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Fix for NullReferenceException in WillMoveToParentViewController When Removing Page During Navigation #27275

Merged
merged 6 commits into from
Jan 25, 2025

Conversation

praveenkumarkarunanithi
Copy link
Contributor

@praveenkumarkarunanithi praveenkumarkarunanithi commented Jan 22, 2025

Root Cause

The NullReferenceException occurs when a non-current page is removed from the navigation stack. Specifically, when RemovePage is invoked for a page that is not the current one, the NavigationPage.Legacy.RemovePage method synchronously calls _removePageRequested?.Invoke(this, new NavigationRequestedEventArgs(page, true)). This invocation triggers the RemovePage method in the NavigationRenderer, which immediately calls Dispose(). The Dispose() method then calls Disconnect(), setting the Tracker to null before the native WillMoveToParentViewController method is executed. As a result, when WillMoveToParentViewController attempts to access the now null Tracker, a NullReferenceException is thrown.

In contrast, removing the current page using PopAsync handles the removal asynchronously. This ensures that WillMoveToParentViewController is called while the Tracker remains valid. Only after the native operations complete does Disconnect(false) nullify the Tracker. This proper sequencing maintains the integrity of the Tracker during native method execution, preventing the exception when using PopAsync.

Description of Change

The fix adds a null check for ToolbarTracker in the WillMoveToParentViewController method, ensuring it is accessed only when initialized, thereby preventing the NullReferenceException.

Issues Fixed

Fixes #25726
Tested the behaviour in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Screenshots

BeforeFix.mov
AfterFix.mov

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jan 22, 2025
Copy link
Contributor

Hey there @praveenkumarkarunanithi! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@vishnumenon2684 vishnumenon2684 added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Jan 22, 2025
@dotnet dotnet deleted a comment from azure-pipelines bot Jan 22, 2025
@jsuarezruiz
Copy link
Contributor

jsuarezruiz commented Jan 22, 2025

/azp run

This comment was marked as outdated.

@praveenkumarkarunanithi praveenkumarkarunanithi marked this pull request as ready for review January 23, 2025 11:56
@Copilot Copilot bot review requested due to automatic review settings January 23, 2025 11:56
@praveenkumarkarunanithi praveenkumarkarunanithi requested a review from a team as a code owner January 23, 2025 11:56

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jfversluis jfversluis added this to the .NET 9 SR4 milestone Jan 25, 2025
@jfversluis jfversluis merged commit 9314fbf into dotnet:main Jan 25, 2025
104 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-navigation NavigationPage community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/iOS 🍎
Projects
None yet
4 participants