Skip to content

[Android] ObjectDisposedException (ContentViewGroup) in BindableLayout.ClearChildren when ItemsSource changes on a hidden layout after the UI thread has been idle #40471

Open

Description

@andreashellquist

Description

On Android, changing BindableLayout.ItemsSource on a layout that is currently hidden (an ancestor has IsVisible="False") can throw ObjectDisposedException from ContentViewHandler.DisconnectHandler / BorderHandler.DisconnectHandler. The page is alive and on screen; only the part of it holding the layout is hidden. The platform view of a template child (Microsoft.Maui.Platform.ContentViewGroup) has already been disposed, while the cross-platform child still has its handler, so DisconnectHandlers calls RemoveAllViews() on a disposed peer.

The failure depends on the main thread having been idle. With a repeating one-second Dispatcher.StartTimer that does nothing, it does not occur.

Stack trace

System.ObjectDisposedException: Cannot access disposed object with JniIdentityHashCode=...
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.ViewGroup.RemoveAllViews()
   at Microsoft.Maui.Handlers.BorderHandler.DisconnectHandler(ContentViewGroup platformView)
   at Microsoft.Maui.Handlers.ViewHandler`2.OnDisconnectHandler(Object platformView)
   at Microsoft.Maui.Handlers.ElementHandler.DisconnectHandler(Object platformView)
   at Microsoft.Maui.Handlers.ElementHandler.Microsoft.Maui.IElementHandler.DisconnectHandler()
   at Microsoft.Maui.ViewExtensions.DisconnectHandlers(IView view)
   at Microsoft.Maui.Controls.BindableLayoutController.ClearChildren(IBindableLayout layout)
   at Microsoft.Maui.Controls.BindableLayoutController.TryAddEmptyView(IBindableLayout layout, IEnumerator& enumerator)
   at Microsoft.Maui.Controls.BindableLayoutController.CreateChildren()
   at Microsoft.Maui.Controls.BindableLayoutController.SetItemsSource(IEnumerable itemsSource)

The same happens through ContentViewHandler.DisconnectHandler when the template root is a ContentView instead of containing a Border.

Where we see it

A page with three sections stacked in one Grid, each shown or hidden with IsVisible (a tab layout). Section 1 is a ScrollView with several BindableLayouts; their item templates contain a Border or are ContentViews.

  1. Show section 1, let it load, scroll it down and back up.
  2. Switch to section 3 (section 1 becomes IsVisible="False").
  3. Wait about 20 seconds without touching the app.
  4. Set the ItemsSource of a BindableLayout in section 1 to null or to a new empty collection.

Result: ObjectDisposedException as above. Expected: the children are removed without an exception.

What we checked

  • 6 of 6 runs fail with these steps; without the scroll in step 1 it did not fail (1 run).
  • Walking the page's visual tree once a second and checking Handle == IntPtr.Zero on every Handler.PlatformView never finds a disposed view, and with that watcher running the exception does not occur (3 of 3 runs clean).
  • A one-second dispatcher timer that does nothing also avoids it (1 run).
  • Replacing the Border in one template with a BoxView only moves the failure to the next BindableLayout in the hidden section.
  • No application code disposes or wraps these native views.

Not done

  • No minimal reproduction project yet; the steps above are from a production app.
  • We have not identified what disposes the ContentViewGroup.

Environment

  • .NET MAUI 10.0.110, net10.0-android, Debug build
  • Android emulator, Pixel, API 36
  • Not checked on iOS or on a physical device

Workaround

Catch ObjectDisposedException around the code that changes the ItemsSource and retry when the section is shown again.

Activity

  1. andreashellquist commented on Nov 20, 2026

    @andreashellquist
    Author

    I have a minimal repro now, and it changes the description above: the view that throws is not on the visible page. It is a view that was abandoned with its handlers still connected and is still bound to a long-lived view model.

    Repro project: https://github.com/andreashellquist/maui-issue-40471-repro (one page, three buttons, about 170 lines)

    Repro (Microsoft.Maui.Controls 10.0.110, net10.0-android, Debug, Mono, emulator API 37 arm64)

    1. A layout with BindableLayout.ItemsSource bound to a singleton view model; the item template contains a Border (or has a ContentView root). BindingContext is set on the layout itself.
    2. Show it, then abandon it without DisconnectHandlers(): remove it from its parent (host.Content = null), or pop its page with HandlerDisconnectPolicy.Manual.
    3. GC.Collect(); Java.Lang.JavaSystem.Gc(); from a background thread (once for the removed view, twice for the popped page).
    4. Set the bound collection to null (or an empty collection) on the view model.

    Result, 3 of 3 runs for each variant:

    Abandoned by Item template Result
    removing the layout from its parent Grid + Label + 1 pt Border ObjectDisposedException in BorderHandler.DisconnectHandler
    removing the layout from its parent ContentView + Label ObjectDisposedException in ContentViewHandler.DisconnectHandler
    removing the layout from its parent Grid + Label + BoxView no exception
    popping the page, HandlerDisconnectPolicy.Manual Border / ContentView same exceptions
    popping the page, default policy Border no exception (the pop disconnects the handlers)

    What happens

    After step 2 the only thing that still reaches the layout is the weak reference in BindingExpression.WeakPropertyChangedProxy. The collection in step 3 takes the Java peers of its views (Handle == 0), but the managed views are still there and the weak reference still resolves. In step 4 the binding applies the new value to the abandoned layout, BindableLayoutController.ClearChildren calls DisconnectHandlers() on the item views, and BorderHandler.DisconnectHandler / ContentViewHandler.DisconnectHandler call RemoveAllViews() on the disposed ContentViewGroup.

    One more GC before step 4 makes the exception go away, because the managed views are then collected too. In a real app it therefore shows up only when the change happens between those two collections, which looks random and timing-dependent (that is what I misread as "the UI thread has been idle" in the original report).

    LayoutHandler.DisconnectHandler on Android appears to check for a disposed platform view before RemoveAllViews() (read on main), which fits the Grid + BoxView variant not throwing. BorderHandler and ContentViewHandler have no such check; adding it there would avoid the exception.

    The exception surfaces in the view model's property setter while a different page is on screen. Hiding the layout, scrolling, waiting, a custom layout, CollectionView and images made no difference in the repro.

    Stack trace from the repro:

    System.ObjectDisposedException: Cannot access disposed object with JniIdentityHashCode=146115326.
       at Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable self)
       at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters)
       at Android.Views.ViewGroup.RemoveAllViews()
       at Microsoft.Maui.Handlers.BorderHandler.DisconnectHandler(ContentViewGroup platformView)
       at Microsoft.Maui.Handlers.ViewHandler.OnDisconnectHandler(Object platformView)
       at Microsoft.Maui.Handlers.ElementHandler.DisconnectHandler(Object platformView)
       at Microsoft.Maui.Handlers.ElementHandler.Microsoft.Maui.IElementHandler.DisconnectHandler()
       at Microsoft.Maui.ViewExtensions.DisconnectHandlers(IView view)
       at Microsoft.Maui.Controls.BindableLayoutController.ClearChildren(IBindableLayout layout)
       at Microsoft.Maui.Controls.BindableLayoutController.TryAddEmptyView(IBindableLayout layout, IEnumerator& enumerator)
       at Microsoft.Maui.Controls.BindableLayoutController.CreateChildren()
       at Microsoft.Maui.Controls.BindableLayoutController.SetItemsSource(IEnumerable itemsSource)
       at Microsoft.Maui.Controls.BindableLayout.<>c.<.cctor>b__25_0(BindableObject b, Object o, Object n)
       ...
       at Microsoft.Maui.Controls.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget, SetterSpecificity specificity)
       at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.PropertyChanged(Object sender, PropertyChangedEventArgs args)
       at Microsoft.Maui.Controls.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(Object sender, PropertyChangedEventArgs e)
       at MauiIssue40471.MainViewModel.set_Items(ObservableCollection`1 value)
    
  2. NafeelaNazhir commented on Nov 20, 2026

    @NafeelaNazhir
    Contributor

    This issue has been validated in Visual Studio Code 1.147.0 with .NET MAUI versions 11.0.10 and 10.0.130 . It can be reproduced on the Android platform.

    Screen.Recording.2026-11-20.at.5.15.44.PM.mov
  3. added
    t/bugSomething isn't working
    area-layoutStackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter
    s/verifiedVerified / Reproducible Issue ready for Engineering Triage
    s/triagedIssue has been reviewed
    on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-layoutStackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenterpartner/syncfusionIssues / PR's with Syncfusion collaborationplatform/androids/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions