Skip to content

[Android][Modal] Fixed crash caused by accessing a disposed Java peer when comparing fragments in OnFragmentDestroyed#809

Merged
Vetle444 merged 4 commits intomainfrom
vefi/androidPopModalCrash
Mar 4, 2026
Merged

[Android][Modal] Fixed crash caused by accessing a disposed Java peer when comparing fragments in OnFragmentDestroyed#809
Vetle444 merged 4 commits intomainfrom
vefi/androidPopModalCrash

Conversation

@Vetle444
Copy link
Contributor

@Vetle444 Vetle444 commented Mar 4, 2026

Description of Change

Description of Change

Guard against disposed Java peer objects in FragmentLifeCycleCallback.OnFragmentDestroyed on Android to prevent a crash when comparing dialog fragments.

Problem

On Android, a crash occurs with Cannot access disposed object with JniIdentityHashCode=... when a modal is dismissed. The OnFragmentDestroyed callback retrieves a DialogFragment from a WeakReference stack and calls Fragment.Equals() — a JNI-bridged method. By the time this callback fires, the underlying Java peer can already be disposed while the managed .NET wrapper is still alive, causing AssertSelf to throw.

Solution

  • Check Handle != IntPtr.Zero on both fragment references before calling .Equals(), since Handle becomes zero when the Java peer is disposed
  • Catch ObjectDisposedException as a safety net for any remaining race window, popping the stack since the fragment is destroyed regardless

Platform Differences

  • Android only — this callback and fragment lifecycle stack are Android-specific

Copilot AI review requested due to automatic review settings March 4, 2026 09:30
@Vetle444 Vetle444 enabled auto-merge (squash) March 4, 2026 09:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses an Android modal crash caused by invoking JNI-backed fragment comparisons (Fragment.Equals) after the underlying Java peer has been disposed, by adding guard checks and a safety-net exception handler in OnFragmentDestroyed.

Changes:

  • Add Handle != IntPtr.Zero checks before comparing DialogFragment instances in OnFragmentDestroyed.
  • Catch ObjectDisposedException during fragment comparison and pop the tracked fragment reference.
  • Add a patch entry to CHANGELOG.md for version 55.2.3.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/library/DIPS.Mobile.UI/API/Library/Android/FragmentLifeCycleCallback.cs Adds disposal guards/exception handling around fragment comparison to prevent JNI disposed-peer crashes when dismissing modals.
CHANGELOG.md Adds a patch release entry documenting the Android modal crash fix.

Copy link
Contributor

Copilot AI commented Mar 4, 2026

@Vetle444 I've opened a new pull request, #811, to work on those changes. Once the pull request is ready, I'll request review from you.

@GovermanGambo
Copy link
Contributor

Alternatively, you can check dialogFragment.PeerReference.IsValid, which is the condition that causes the exception internally

@Vetle444 Vetle444 merged commit c469bda into main Mar 4, 2026
1 check passed
@Vetle444 Vetle444 deleted the vefi/androidPopModalCrash branch March 4, 2026 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants