Xamarin.forms: [Bug] System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'GroupableItemsViewController`1'

Created on 6 Nov 2019  ·  19Comments  ·  Source: xamarin/Xamarin.Forms

Description

On iOS a Shell App with CollectionView crashes. Android works.

Steps to Reproduce

  1. Login and GoTo //main/computers/root which loads the page with a CollectionView (computer:ComputersRootPage).
  2. Tap on TabBar to AccountPage (local:AccountPage)
  3. Tap on TabBar to computer:ComputersRootPage (like in 1st step)
  4. App crashes.

Shell AppShell.xaml structure:

<ShellItem Route="login">
    <ShellContent ContentTemplate="{DataTemplate local:LoginPage}"/>
</ShellItem>

<TabBar Route="main">
    <Tab Title="Computers" Icon="computers.png" Route="computers">
        <ShellContent ContentTemplate="{DataTemplate computer:ComputersRootPage}" 
                      Title="Root"
                      Route="root"
                      />
    </Tab>
    <Tab Title="Favorites" Icon="favorites.png">
        <ShellContent ContentTemplate="{DataTemplate local:FavoritesPage}" />
    </Tab>
    <Tab Title="Active" Icon="active.png">
        <ShellContent ContentTemplate="{DataTemplate local:ActivePage}" />
    </Tab>
    <Tab Title="Users" Icon="users.png">
        <ShellContent ContentTemplate="{DataTemplate local:UsersPage}" />
    </Tab>
    <Tab Title="Account" Icon="account.png">
        <ShellContent ContentTemplate="{DataTemplate local:AccountPage}" />
    </Tab>
</TabBar>

ComputersRootPage XAML structure

<?xml version="1.0" encoding="utf-8"?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:forms="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms"
             Style="{StaticResource ContentPageBase}"
             mc:Ignorable="d"
             x:Class="Next.Remoted.Views.Computers.ComputersRootPage"
             Shell.NavBarIsVisible="True"
             x:Name="Self">

    <Shell.TitleView>
        <Label x:Name="TitleView"
               Opacity="0"
               HorizontalTextAlignment="Center"
               VerticalTextAlignment="Center"
               HorizontalOptions="CenterAndExpand"
               FontAttributes="Bold"
               TextColor="{StaticResource AppBlue}"
               Text="{Binding ComputersTitleText.Result}" />
    </Shell.TitleView>
    <ContentPage.ToolbarItems>
        <ToolbarItem IconImageSource="search" Command="{Binding SearchCommand}" />
    </ContentPage.ToolbarItems>


    <RefreshView IsRefreshing="{Binding IsRefreshing}"
                 Command="{Binding InitialLoadCommand}">
        <CollectionView x:Name="ComputersRootFolderView"
                        ItemsSource="{Binding RemotedEntries}"
                        SelectionMode="Single"
                        SelectionChanged="SelectableItemsView_OnSelectionChanged"
                        Scrolled="ItemsView_OnScrolled">
            <CollectionView.Header>
                <StackLayout HeightRequest="100">
                    <Label x:Name="HeaderTitle"
                           Text="{Binding Source={x:Reference Self}, Path=BindingContext.ComputersTitleText.Result}"
                           HorizontalOptions="CenterAndExpand"
                           Margin="0,20,0,0"
                           Style="{StaticResource ComputersPageLabel}" />
                    <Label
                        Text="{Binding Source={x:Reference Self}, Path=BindingContext.RemotedEntries.Count, StringFormat='{0}'}"
                        HorizontalTextAlignment="Center"
                        TextColor="{StaticResource AppLightBlue}"
                        Margin="5,10,0,10" />
                    <BoxView HeightRequest="1" HorizontalOptions="FillAndExpand" Color="{StaticResource AppBlue}" />
                </StackLayout>
            </CollectionView.Header>
            <CollectionView.EmptyView>
                <ActivityIndicator  IsRunning="True"
                                    Color="{StaticResource AppBlue}"
                                    BackgroundColor="{StaticResource AppDarkBlue}" />
            </CollectionView.EmptyView>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid Padding="0,5,0,0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <forms:SvgCachedImage
                            Grid.Column="0"
                            WidthRequest="30"
                            HeightRequest="30"
                            Style="{StaticResource FfimageLandingStyle}"
                            Source="resource://Next.Remoted.Resources.folder_closed.svg" />

                        <Label
                            Grid.Column="1"
                            Style="{StaticResource ComputersPageLabel}"
                            Text="{Binding Name}" />

                        <forms:SvgCachedImage
                            Grid.Column="2"
                            WidthRequest="{OnPlatform Default=30,iOS=40}"
                            HeightRequest="{OnPlatform Default=30,iOS=40}"
                            Style="{StaticResource FfimageLandingStyle}"
                            Source="resource://Next.Remoted.Resources.more_static.svg">
                            <forms:SvgCachedImage.GestureRecognizers>
                                <TapGestureRecognizer
                                    Command="{Binding Path=BindingContext.ShowMoreCommand, Source={x:Reference Name=ComputersRootFolderView}}"
                                    CommandParameter="{Binding .}"
                                    NumberOfTapsRequired="1" />
                            </forms:SvgCachedImage.GestureRecognizers>
                        </forms:SvgCachedImage>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </RefreshView>
</ContentPage>

Expected Behavior

No crash.

Actual Behavior

iOS App crashes.

System.ObjectDisposedException: 'Cannot access a disposed object.
Object name: 'GroupableItemsViewController`1'

Basic Information

XF SDK Version: 4.3.0.947036 (latest stable)
IDE: Visual Studio 2019
Platform Target Frameworks:
iOS: 13.1, 13.2 (Probbably other versions too)
App Type: Xamarin.Forms

needs-info ❓ needs-repro ❔ unverified bug

Most helpful comment

I can reproduce with iOS 12.2 and Xamarin.Forms 4.7.0.1239

All 19 comments

@pauldipietro based on your last blog post https://devblogs.microsoft.com/xamarin/xamarin-forms-4-3-introducing-collectionview/
I developed my production app with the CollectionView. Because you wrote it is not experimental anymore. I appreciate your posts, but please take more care with such announcements. Now I have to revert back to ListView views.

@ericbrunner I'm trying to reproduce this error, but I'm not having much luck so far. Can you post a minimal repro project which demonstrates it?

Fix pls

@kcrg @ericbrunner

  • can you provide a repro? and/or provide a full stack trace? Though a repro would help us fix this super quick
  • test with the latest?

@PureWeen All that information is inside my comment from 6th of November. Create a new Xamarin.Forms Shell API without REST API Backend. Update the data-bindings in the XAML. Adjust the AppShell.xaml to the structure I posted. I will try to create such repo

@ericbrunner aw I had missed that there was an existing issue this linked to

So can we just close this one in favor of https://github.com/xamarin/Xamarin.Forms/issues/8308 . ?

Seems like that one already has a repro and is confirmed

@PureWeen No you can't close this issue until solved. The other issue is different. There is mentioned that there is no issue on tabs. Well my filed issue is totally tabbar related / collectionview issue on iOS.

Sounds good! Though I'm fairly confident once that one is solved it'll fix this one :-)

In either case, a full stack trace would also be helpful

I'll create a repo tomorrow and upload it

@PureWeen

Ok i created a repo. In the meanwhile new Xamarin.Forms SDK versions were released. I updated to

Xamarin.Forms SDK: 4.4.0.936621-pre1

The iOS App doesn't crash now anymore, which is an improvement. But there are still related Debug Log outputs.

You can reproduce it when you pull down to refresh.

2019-12-08 07:57:00.943926+0100 ShellHotrestartAppTest.iOS[96794:8599051] [UICollectionView] Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). - will perform reloadData. UICollectionView instance: <UICollectionView: 0x7f983443b000; frame = (0 0; 414 725); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600000e809c0>; layer = <CALayer: 0x60000073d100>; contentOffset: {0, -160}; contentSize: {414, 45}; adjustedContentInset: {160, 0, 0, 0}; layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7f9832e35810>; dataSource: <Xamarin_Forms_Platform_iOS_GroupableItemsViewController_1: 0x7f9832e488c0>>; currentUpdate: [UICollectionViewUpdate 
- 0x7f9832e6ddf0: old:<UICollectionViewData: 0x6000039c0d20> new<UICollectionViewData: 0x60000393ed80> items:<(
    "I(0,0)"
)>]

ios_collectionview
ShellHotrestartAppTest.zip

The latest warnings should be fixed by: https://github.com/xamarin/Xamarin.Forms/pull/7711

@jsuarezruiz In that case I close that issue

Please re-open this issue, we receive this error Inserting items into an ObservableCollection on iOS Simulator, running Xamarin.Forms 4.4.0.991265

Guys, we are experiencing the issue on iOS, on last stable version of XF related to navigation

2020-02-08 01:44:44.737620+0200 IRA.iOS[27782:209679] Unhandled managed exception: Cannot access a disposed object. Object name: 'ParentingViewController'. (System.ObjectDisposedException) at Foundation.NSObject.get_SuperHandle () [0x00012] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.10.0.17/src/Xamarin.iOS/Foundation/NSObject2.cs:449 at UIKit.UIViewController.get_NavigationItem () [0x0002a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.10.0.17/src/Xamarin.iOS/UIViewController.g.cs:2303 at Xamarin.Forms.Platform.iOS.NavigationRenderer+<>c__DisplayClass76_0.<SetMasterLeftBarButton>b__0 (UIKit.UIImage icon) [0x0002c] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:761 at Xamarin.Forms.Platform.iOS.ImageElementManager.ApplyNativeImageAsync (Xamarin.Forms.BindableObject bindable, Xamarin.Forms.BindableProperty imageSourceProperty, System.Action1[T] onSet, System.Action1[T] onLoading, System.Threading.CancellationToken cancellationToken) [0x000fd] in D:\a \1\s\Xamarin.Forms.Platform.iOS\Renderers\ImageElementManager.cs:281 at Xamarin.Forms.Platform.iOS.NavigationRenderer.SetMasterLeftBarButton (UIKit.UIViewController containerController, Xamarin.Forms.MasterDetailPage masterDetailPage) [0x0007f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:747 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.10.0.17/src/Xamarin.iOS/Foundation/NSAction.cs:178 --- End of stack trace from previous location where exception was thrown ---

2020-02-08 01:27:04.306090+0200 IRA.iOS[24737:184880] Unhandled managed exception: Cannot access a disposed object. Object name: 'PrimaryToolbarItem'. (System.ObjectDisposedException) at Foundation.NSObject.get_SuperHandle () [0x00012] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.10.0.17/src/Xamarin.iOS/Foundation/NSObject2.cs:449 at UIKit.UIBarButtonItem.set_Image (UIKit.UIImage value) [0x00033] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.10.0.17/src/Xamarin.iOS/UIBarButtonItem.g.cs:652 at Xamarin.Forms.Platform.iOS.ToolbarItemExtensions+PrimaryToolbarItem.UpdateIconAndStyle () [0x00031] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Extensions\ToolbarItemExtensions.cs:84 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 at Foundation.NSAsyncSynch ronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.10.0.17/src/Xamarin.iOS/Foundation/NSAction.cs:178 --- End of stack trace from previous location where exception was thrown ---

@PureWeen @hartez Please fix this critical bug.

I also get it randomly.

I can reproduce this 2 of 3 times 😢 with iOS v12.4 & Xamarin.Forms v4.7.0.1080, but in iOS v13.5 & v13.6 I can't reproduce it🧐

Update:

With version Xamarin.Forms 4.6.0.1180 this bug is not reproducible at iOS v12.4

I can reproduce with iOS 12.2 and Xamarin.Forms 4.7.0.1239

I have this error. Xamarin Team, any news?

Was this page helpful?
0 / 5 - 0 ratings