Skip to content

ObjectDisposedException for Android ContentViewGroup when closing page (since 9.0.40) #29711

@markusksofa

Description

@markusksofa

Description

When closing a page and returning to the previous page in the navigation stack (NavigationPage), the Android App sometimes crashes with the following fatal exception:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Microsoft.Maui.Platform.ContentViewGroup'.
   at Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable self)
   at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters)
   at Android.Views.View.RemoveOnAttachStateChangeListener(IOnAttachStateChangeListener listener)
   at Android.Views.View.<>c__DisplayClass3033_0.<remove_ViewDetachedFromWindow>b__0(IOnAttachStateChangeListener __v)
   at Java.Interop.EventHelper.RemoveEventHandler[IOnAttachStateChangeListener,IOnAttachStateChangeListenerImplementor](WeakReference& implementor, Func`2 empty, Action`1 unsetListener, Action`1 remove)
   at Android.Views.View.remove_ViewDetachedFromWindow(EventHandler`1 value)
   at Microsoft.Maui.Platform.ViewExtensions.<>c__DisplayClass43_0.<OnUnloaded>b__1()
   at Microsoft.Maui.Platform.ActionDisposable.Dispose()
   at Microsoft.Maui.Platform.ViewExtensions.<>c__DisplayClass43_0.<OnUnloaded>b__3()
   at Java.Lang.Thread.RunnableImplementor.Run()
   at Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz)

It only happens sometimes, so it seems that the ContentViewGroup is not disposed correctly sometimes.

When closing a certain page in our production App, opening and closing it again, until the bug happens, it sometimes needs from a few seconds to a few minutes. It's more likely to happen on fast devices. Only on Android.

Seems to be similar to #28051, but for a different component.

Steps to Reproduce

I was not able to create a repro project yet, just tried it with a simple AppFlyout app, but the bug did not appear.
So if you don't know this bug anyway, I might need to add more functionality until it happens when I have time later.

Link to public reproduction project repository

No response

Version with bug

9.0.40 SR4

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

9.0.30 SR3

Affected platforms

Android

Affected platform versions

Android 11, 14, 15 (probably all)

Did you find any workaround?

Using MAUI version 9.0.30
Bug still occurs in 9.0.70

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    s/needs-reproAttach a solution or code which reproduces the issuet/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions