Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] [iOS] Cannot access a disposed object. Object name: 'GroupableItemsViewController`1 #8308

Closed
Genfood opened this issue Oct 30, 2019 · 91 comments
Assignees
Labels
a/collectionview i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often m/high impact ⬛ p/iOS 🍎 t/bug 🐛

Comments

@Genfood
Copy link

Genfood commented Oct 30, 2019

Description

A collection view with header causes a crash after navigating via the shell flyout.
The bug is iOS only. On Android it works fine.

The app wont crash if you use tabs.

Steps to Reproduce

  1. Open the App
  2. Use the shell flyout to navigate to Page 2
  3. Use the shell flyout to navigate back to Page 1

--> The App will crash

Expected Behavior

Not crashing.

Actual Behavior

Throws:

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

Basic Information

  • Version with issue: 4.3.0.947036, 4.3.0.908675, ..., 4.4.0.991265
  • Last known good version: 4.2.0.910310
  • IDE: Visual Studio 2019
  • Platform Target Frameworks:
    • iOS: 13.1, 13.2 (Probbably other versions too)

Reproduction Link

BugExample.zip

@Genfood Genfood added s/unverified New report that has yet to be verified t/bug 🐛 labels Oct 30, 2019
@pauldipietro pauldipietro added this to New in Triage Oct 30, 2019
@hartez hartez added a/collectionview p/iOS 🍎 a/shell 🐚 and removed s/unverified New report that has yet to be verified labels Oct 30, 2019
@hartez hartez added this to To do in iOS Ready For Work via automation Oct 30, 2019
@hartez hartez added this to Backlog in CollectionView via automation Oct 30, 2019
@hartez hartez removed this from New in Triage Oct 30, 2019
@Genfood
Copy link
Author

Genfood commented Oct 31, 2019

Any ideas for a workaround?

@ericbrunner
Copy link

ericbrunner commented Nov 4, 2019

Same behaviour with a TabBar. Is there any investigation ongoing?

AppShell.xaml

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

<!-- Your Pages -->
<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>

Debug Log

[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: 0x7fe2e8146e00; frame = (0 0; 414 725); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60000271e880>; layer = <CALayer: 0x600002c18480>; contentOffset: {0, -100}; contentSize: {414, 45}; adjustedContentInset: {100, 0, 0, 0}; layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7fe2eab0c7d0>; dataSource: <Xamarin_Forms_Platform_iOS_GroupableItemsViewController_1: 0x7fe2eab0cb00>>; currentUpdate: [UICollectionViewUpdate - 0x7fe2e5e1
c620: old:<UICollectionViewData: 0x6000010d50a0> new<UICollectionViewData: 0x6000010548c0> items:<(
"I(0,0)"
)>]

Exception Message

Cannot access a disposed object.
Object name: GroupableItemsViewController

@BioTurboNick
Copy link

I just got this same error from an otherwise-working app revision that has been out for a month.

Occurred on iOS 12.4.1. Report from AppCenter, so details limited and I haven't been able to reproduce it. Most people seem to never encounter it.

@Genfood
Copy link
Author

Genfood commented Nov 5, 2019

@BioTurboNick which version of Xamarin Forms do you use? Because your problem looks like something else.
The error above is pretty easy to reproduce and will happen every time you switch between pages.

@BioTurboNick
Copy link

Hmm maybe so. I'm on 4.2.0.848062. Still, I had never seen an ObjectDisposedException from GroupableItemsViewController before now, ever.

@Genfood
Copy link
Author

Genfood commented Nov 5, 2019

I tested this bug on 4.2.0.9... and it worked fine. Probably this is something else?

@ericbrunner
Copy link

Regarding XF SDK Version: 4.3.0.947036 (latest stable)

@ericbrunner
Copy link

I filed a new issue because it's related but happens on a TabBar: #8394

@eramrit78
Copy link

same issue come but without grouping. in forms version 4.3

@digitalninjae
Copy link

I had the problem with a collection view with a footer. Removing the footer appears to have resolved the problem.

@daves1992
Copy link

Still getting this issue on 4.4.0.991265 I don't have a header or footer in my CollectionView and it still crashes.

Untitled

@ChrisAllisonMalta
Copy link

I get this bug with a simple (I think) collection view, I'm using Xamarin Forms 4.4.0.991265, with a listview it works fine

  <CollectionView Grid.Row="0" ItemsSource="{Binding Currencies}" IsGrouped="False">
                <CollectionView.ItemsLayout>
                   <GridItemsLayout Orientation="Horizontal" />
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid Padding="10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="100" />
                                <RowDefinition Height="100" />
                            </Grid.RowDefinitions>
                          
                       
                            <Label Grid.Row="0" 
                                   Text="{Binding CCY}" 
                                   FontAttributes="Bold"
                                   FontFamily="AppleSDGothicNeo-UltraLight" 
                                   />
                            <Label Grid.Row="1"
                                   FontFamily="AppleSDGothicNeo-UltraLight" 
                                  
                                   TextColor="{Binding Path=Amount, Converter={StaticResource ValueToColorConverter}}"
                                   Text="{Binding ., Converter={StaticResource CurrencyConverter}}"
                              
                                    />
                        </Grid>

                    </DataTemplate>
                </CollectionView.ItemTemplate>
        </CollectionView>

It renders the first time but when I do a 'hot reload' it breaks with:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'GroupableItemsViewController1'. at Foundation.NSObject.get_SuperHandle () [0x00012] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.2.0.42/src/Xamarin.iOS/Foundation/NSObject2.cs:449 at UIKit.UIViewController.get_IsViewLoaded () [0x00023] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.2.0.42/src/Xamarin.iOS/UIViewController.g.cs:2075 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.NotLoadedYet () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:142 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.Add (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x0000d] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:147 at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00023] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:100 at (wrapper delegate-invoke) <Module>.invoke_void_object_NotifyCollectionChangedEventArgs(object,System.Collections.Specialized.NotifyCollectionChangedEventArgs) at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263
at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:338 at System.Collections.ObjectModel.ObservableCollection1[T].InsertItem (System.Int32 index, T item) [0x0001a] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:196
at System.Collections.ObjectModel.Collection`1[T].Add (T item) [0x00020] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/external/corefx/src/Common/src/CoreLib/System/Collections/ObjectModel/Collection.cs:71
at O.ViewModels.CashSummaryViewModel.ExecueLoadItemsCommand (System.Boolean ForceRefresh) [0x00103] in /Users/chris/Projects/O/ViewModels/CashSummaryViewModel.cs:43
2019

@digitalninjae
Copy link

After updating to 4.4.0.991265, I'm running into this again - even on collection views without headers or footers. :(

@samhouts samhouts added this to Backlog in Shell Jan 3, 2020
@g4mb10r
Copy link

g4mb10r commented Feb 4, 2020

Please fix this urgently. I also have this issue. Cannot use shell in production because of it. Should be marked critical.

Steps to Reproduce
Open the App
Use the shell flyout to navigate to Page 2
Use the shell flyout to navigate back to Page 1
--> The App will crash

@jsiemens
Copy link

Same problem. Why is this not fixed yet?

@g4mb10r
Copy link

g4mb10r commented Feb 12, 2020

What @jsiemens said 👆

@gabrielfreire
Copy link

It's quite impressive this wasn't fixed yet, i'm also having this bug, the CollectionView never worked properly on iOS

@mduchev
Copy link

mduchev commented Oct 5, 2020

@l0gaw 's workaround used to work, but now it doesn't. Can't tell if it is from the last iOS update, or not, but it is getting really annoying - crashes everywhere from simple update/clear.

@lafritay
Copy link

lafritay commented Oct 7, 2020

@mduchev I'm seeing this as well. It's happening on both iOS 13 and 14. The only thing that changed for me was moving to Xamarin.iOS 14. Trying to see if I can come up with a workaround now.

Specifically, here's the stack I see now:

NSObject.get_SuperHandle ()
UIViewController.get_IsViewLoaded ()
ObservableItemsSource.NotLoadedYet ()
ObservableItemsSource.ReloadRequired ()
ObservableItemsSource.Remove (System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
ObservableItemsSource.CollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state)
NSAsyncSynchronizationContextDispatcher.Apply ()
(wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate)
UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName)
Application.Main (System.String[] args)

@rd09
Copy link

rd09 commented Oct 9, 2020

After publishing out the suggested workaround, I am now seeing this exception in App Center: System.IndexOutOfRangeException: IItemsViewSource is empty

Xamarin.Forms.Platform.iOS
EmptySource.GetIndexForItem (System.Object item)
Xamarin.Forms.Platform.iOS
ItemsViewController`1[TItemsView].GetIndexForItem (System.Object item)
Xamarin.Forms.Platform.iOS
SelectableItemsViewController`1[TItemsView].SelectItem (System.Object selectedItem)
Xamarin.Forms.Platform.iOS
SelectableItemsViewController`1[TItemsView].UpdateNativeSelection ()
Xamarin.Forms.Platform.iOS
SelectableItemsViewRenderer`2[TItemsView,TViewController].UpdateNativeSelection ()
Xamarin.Forms.Platform.iOS
SelectableItemsViewRenderer`2[TItemsView,TViewController].UpdateItemsSource ()
Xamarin.Forms.Platform.iOS
ItemsViewRenderer`2[TItemsView,TViewController].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs changedProperty)
Xamarin.Forms.Platform.iOS
StructuredItemsViewRenderer`2[TItemsView,TViewController].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs changedProperty)
Xamarin.Forms.Platform.iOS
SelectableItemsViewRenderer`2[TItemsView,TViewController].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs changedProperty)
Xamarin.Forms.Platform.iOS
GroupableItemsViewRenderer`2[TItemsView,TViewController].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs changedProperty)
(wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
Xamarin.Forms
BindableObject.OnPropertyChanged (System.String propertyName)
Xamarin.Forms
Element.OnPropertyChanged (System.String propertyName)
Xamarin.Forms
BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent)
Xamarin.Forms
BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes)
Xamarin.Forms
BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess)
Xamarin.Forms
ItemsView.set_ItemsSource (System.Collections.IEnumerable value)
FieldEdge.Mobile.iOS.CustomRenderers
CustomCollectionViewRenderer.Dispose (System.Boolean disposing)
Foundation
NSObject.Dispose ()
Xamarin.Forms.Platform.iOS
VisualElementPackager.Dispose (System.Boolean disposing)
Xamarin.Forms.Platform.iOS
VisualElementRenderer`1[TElement].Dispose (System.Boolean disposing)
Foundation
NSObject.Dispose ()
Xamarin.Forms.Platform.iOS
VisualElementPackager.Dispose (System.Boolean disposing)
Xamarin.Forms.Platform.iOS
VisualElementRenderer`1[TElement].Dispose (System.Boolean disposing)
Foundation
NSObject.Dispose ()
Xamarin.Forms.Platform.iOS
VisualElementPackager.Dispose (System.Boolean disposing)
Xamarin.Forms.Platform.iOS
VisualElementRenderer`1[TElement].Dispose (System.Boolean disposing)
Foundation
NSObject.Dispose ()
Xamarin.Forms.Platform.iOS
VisualElementPackager.Dispose (System.Boolean disposing)
Xamarin.Forms.Platform.iOS
VisualElementRenderer`1[TElement].Dispose (System.Boolean disposing)
Foundation
NSObject.Dispose ()
Xamarin.Forms.Platform.iOS
DisposeHelpers.DisposeModalAndChildRenderers (Xamarin.Forms.Element view)
Xamarin.Forms.Platform.iOS.Platform.Xamarin.Forms
INavigation.PopModalAsync (System.Boolean animated)
Xamarin.Forms
Application+NavigationImpl.OnPopModal (System.Boolean animated)
FieldEdge.Mobile.Shared.MVVMFramework
NavigationService+<>c__DisplayClass8_0.<CloseModal>b__0 ()

@mduchev
Copy link

mduchev commented Oct 9, 2020

Are you removing an item from the collection? If so, then you may have hit another reported issue (which I am also hitting constantly) - #9632. If not, then I suppose that, as I said above, the workaround isn't working in all of the cases, unfortunately.

@rd09
Copy link

rd09 commented Oct 9, 2020

I was able to recreate the last issue I posted. When I navigate to a screen where I have a CollectionView and I modify it by either adding or removing an item, then closing the screen this exception is thrown. If I go to the same screen and don't modify the CollectionView, then exit the screen the exception is not thrown. The workaround is still working for me, though, I did have to refactor some areas because clearing the CollectionView will crash. So, the workaround will definitely introduce new issues. I think the safest bet at this point is not to use CollectionViews.

@okproject
Copy link

okproject commented Oct 13, 2020

I have same issue.
Scenario is

  • Create a carouselview
  • Use collectionview inside carouselview template
  • For example click carousel items and collection items; make them states changed - cliecked
  • Try to update some xaml part and check if view updated via hot reload
  • Click carouselview - collectionview items again

At the point, error throws on my experience.

  • It happens on debug mode with ios simulator
  • But i could not reproduce it on the iPhone device deployed with release mode.
  • Error is the case only in debug mode for me.

@mduchev
Copy link

mduchev commented Oct 13, 2020

It would be a lot easier to fix if someone can provide a sample project with the latest Xamarin packages. I can't seem to be able to pinpoint the exact case when this happens constantly. If someone can, please provide a demo project describing the issue so that it can be fixed quickly.

@lafritay
Copy link

@mduchev Did you see this comment from @daves1992?

#8308 (comment)

Just asking since it is somewhat buried in the flood of comments. Does it not reproduce the issue for you?

@mduchev
Copy link

mduchev commented Oct 13, 2020

@lafritay Unfortunately, no. I am able to launch the project on both a simulator & device without any issues. Navigating between the pages also doesn't seem to trigger the exception.

@daves1992
Copy link

@lafritay Unfortunately, no. I am able to launch the project on both a simulator & device without any issues. Navigating between the pages also doesn't seem to trigger the exception.

Which version of IOS?

I managed to replicate it on 12.4.1 but I don't think it replicated on later versions.

@rd09
Copy link

rd09 commented Oct 13, 2020

It would be a lot easier to fix if someone can provide a sample project with the latest Xamarin packages. I can't seem to be able to pinpoint the exact case when this happens constantly. If someone can, please provide a demo project describing the issue so that it can be fixed quickly.

You have to be on iOS 12 to recreate.

@mduchev
Copy link

mduchev commented Oct 13, 2020

@rd09 Not true. I was seeing the same error on iOS 13 devices and now I'm seeing it again on iOS 14 devices. Unfortunately, the sample projects don't seem to recreate the issue so I suppose that it still persists in some other shape.

@softlion
Copy link
Contributor

softlion commented Oct 16, 2020

repro + solution: #11853

no workaround.

@lafritay
Copy link

lafritay commented Oct 16, 2020

@mduchev @rd09 I was able to get a simple repro:

Bugs.zip

Once you load the app, wait for the items to load. Then do the following:

  1. Navigate to the About page using the menu
  2. Navigate back to the items page (it should be blank)
  3. Use "pull to refresh" to refresh the page. The exception should happen when the items finish loading.

The key to making this happen is this line in ItemsPage.xaml.cs

        protected override void OnDisappearing()
        {
            ItemsCollectionView.ItemsSource = null;
            base.OnDisappearing();
        }

The problem is that if CollectionView.ItemsSource is ever updated, the ItemsViewSource that contains the original value never gets disposed. Thus, it retains its CollectionChanged handler and reacts to changes. If the collection then gets updated after navigating away from the page (say, because of a slow network call), the bad ItemsViewSource accesses the disposed ItemsViewController.

I believe the issue is the UpdateItemsSource method in this file: https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewController.cs. It does not dispose of the value held by ItemsSource though it appears it owns the lifetime of the object.

This seems to be confirmed by the fact that it's Android counterpart does dispose of the object (see UpdateItemsSource in this file: https://github.com/xamarin/Xamarin.Forms/blob/79cc0f49fe90a59f02aa0490072b449ccdad4a27/Xamarin.Forms.Platform.Android/CollectionView/ItemsViewAdapter.cs)

And, I think one of the big reasons why this continues to happen is that the workaround that many people are using nulls out the ItemsSource when dispose is called.

@jormenjanssen
Copy link

jormenjanssen commented Oct 21, 2020

@lafritay Im using this workaround which seems to work in short testing. This is not fixing the dispose entirely when the collection source is change correct, but prevents the crashes I had

using Example.Controls;
using Example.iOS.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ExtendedCollectionView), typeof(ExtendedCollectionViewRender))]
namespace Example.iOS.Renderers
{
    public class ExtendedCollectionViewRender : CollectionViewRenderer
    {
        protected override GroupableItemsViewController<GroupableItemsView> CreateController(GroupableItemsView itemsView, ItemsViewLayout layout)
        {
            return new CustomGroupableItemsViewController<GroupableItemsView>(itemsView, layout);
        }
    }
}

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

namespace Example.iOS.Renderers
{
    public class CustomGroupableItemsViewController<TItemsView> : GroupableItemsViewController<TItemsView>
        where TItemsView : GroupableItemsView
    {
        public CustomGroupableItemsViewController(TItemsView selectableItemsView, ItemsViewLayout layout) : base(selectableItemsView, layout)
        {
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            ItemsSource = new EmptySource();
        }
    }
}

using System;
using Foundation;
using Xamarin.Forms.Platform.iOS;

namespace Example.iOS.Renderers
{
    internal class EmptySource : IItemsViewSource
    {
        public int GroupCount => 0;

        public int ItemCount => 0;

        public bool Loop { get ; set; }

        public int LoopCount => 0;

        public object this[NSIndexPath indexPath] => throw new IndexOutOfRangeException("IItemsViewSource is empty");

        public int ItemCountInGroup(nint group) => 0;

        public object Group(NSIndexPath indexPath)
        {
            throw new IndexOutOfRangeException("IItemsViewSource is empty");
        }

        public NSIndexPath GetIndexForItem(object item)
        {
            throw new IndexOutOfRangeException("IItemsViewSource is empty");
        }

        public void Dispose()
        {
        }
    }
}


@lafritay
Copy link

@jormenjanssen Here's my full workaround:

using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

namespace myTIPreport.iOS
{
    /// <summary>
    /// Needed because of this bug: https://github.com/xamarin/xamarin.forms/issues/10842. And this bug:
    /// https://github.com/xamarin/Xamarin.Forms/issues/9691
    /// </summary>
    internal sealed class DefaultCollectionItemsViewController : GroupableItemsViewController<GroupableItemsView>
    {
        public DefaultCollectionItemsViewController(GroupableItemsView itemsView, ItemsViewLayout itemsLayout)
            : base(itemsView, itemsLayout)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Needed because of this bug: https://github.com/xamarin/Xamarin.Forms/issues/9691
            CollectionView.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Automatic;
        }

        public override void UpdateItemsSource()
        {
            // Needed because of this bug: https://github.com/xamarin/Xamarin.Forms/issues/8308#issuecomment-710138958
            ItemsSource?.Dispose();
            base.UpdateItemsSource();
        }

        protected override UICollectionViewDelegateFlowLayout CreateDelegator() =>
            new DefaultCollectionItemsViewDelegator(ItemsViewLayout, this);
    }
}
using CoreGraphics;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

namespace myTIPreport.iOS
{
    /// <summary>
    /// Only needed because of this bug: https://github.com/xamarin/xamarin.forms/issues/10842
    /// </summary>
    internal sealed class DefaultCollectionItemsViewDelegator : SelectableItemsViewDelegator<GroupableItemsView, DefaultCollectionItemsViewController>
    {
        public DefaultCollectionItemsViewDelegator(
            ItemsViewLayout itemsLayout,
            DefaultCollectionItemsViewController itemsController)
            : base(itemsLayout, itemsController)
        {
        }

        /// <summary>
        /// Per default this method returns the Estimated size when its not overriden. This method is called
        /// before the rendering process and sizes the cell correctly before it is displayed in the
        /// CollectionView. Calling the base implementation of this method will throw an exception when
        /// overriding the method.
        /// </summary>
        public override CGSize GetSizeForItem(
            UICollectionView collectionView,
            UICollectionViewLayout layout,
            NSIndexPath indexPath)
        {
            // CellForItem() is not reliable here because when the cell at indexPath is not visible it will
            // return null.
            UICollectionViewCell cell = collectionView.CellForItem(indexPath);
            if (cell is ItemsViewCell itemsViewCell)
            {
                // Get the real cell size.
                return itemsViewCell.Measure();
            }
            else
            {
                // This is basically a fallback when CellForItem() returns null.
                return ItemsViewLayout.EstimatedItemSize;
            }
        }
    }
}

using myTIPreport.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(CollectionView), typeof(DefaultCollectionViewRenderer))]

namespace myTIPreport.iOS
{
    /// <summary>
    /// Reasons for this:
    /// 1. This bug: https://github.com/xamarin/xamarin.forms/issues/10842
    /// 2. This bug: https://github.com/xamarin/Xamarin.Forms/issues/8308
    /// </summary>
    internal class DefaultCollectionViewRenderer : GroupableItemsViewRenderer<GroupableItemsView, DefaultCollectionItemsViewController>
    {
        protected override DefaultCollectionItemsViewController CreateController(
            GroupableItemsView itemsView,
            ItemsViewLayout itemsLayout
        ) => new DefaultCollectionItemsViewController(itemsView, itemsLayout);

        protected override void Dispose(bool disposing)
        {
            // Workaround for this bug: https://github.com/xamarin/Xamarin.Forms/issues/8308
            ItemsView.SelectedItem = null;
            ItemsView.ItemsSource = null;

            base.Dispose(disposing);
        }
    }
}

@reydev-sourcecoder
Copy link

I found a workaround for this bug. I create a custom renderer for Collectionview and I overrode the method Dispose.
Here is the custom renderer code:
[assembly: ExportRenderer(typeof(CollectionView), typeof(CustomCollectionViewRenderer))]
namespace Project.iOS.Renderers
{
public class CustomCollectionViewRenderer : CollectionViewRenderer
{
protected override void Dispose(bool disposing)
{
base.ItemsView.SelectedItem = null;
base.ItemsView.ItemsSource = null;
base.Dispose(disposing);
}
}
}

@l0gaw Do I need to implement only CustomRenderer in the iOS project or do I need something else in the main project?

This workaround worked in my case. I did remove also the line "ItemsView.SelectedItem = null" because it is not needed in my case just like what @rd09 said.

@ahmedroshdy
Copy link

ahmedroshdy commented Oct 31, 2020

same issue happened with me, any solution or workaround ?... this is the stack trace ...
i am using latest stable version from xamarin forms
using AppShell
happens in iOS 12
NSObject.get_SuperHandle ()
UICollectionViewController.get_CollectionView ()
ItemsViewController`1[TItemsView].GetPrototype ()
ItemsViewLayout.DetermineCellSize ()
ListViewLayout.ConstrainTo (CoreGraphics.CGSize size)
ItemsViewLayout.UpdateConstraints (CoreGraphics.CGSize size)
ItemsViewLayout.InvalidateLayout ()
(wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate)
UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName)
Application.Main (System.String[] args)

@hartez
Copy link
Contributor

hartez commented Nov 11, 2020

BugExample.zip

@rmarinho I managed to replicate it by changing the sample slightly.

I added a 100ms delay to setting the Items property.

Iphone 6
IOS 12.4.5
Xamarin.Forms 4.8

This repro no longer crashes as of 4.8.0.1364.

@hartez
Copy link
Contributor

hartez commented Nov 11, 2020

@mduchev @rd09 I was able to get a simple repro:

Bugs.zip

This repro no longer throws the ObjectDisposedException in 5.0.0-pre3.

@hartez
Copy link
Contributor

hartez commented Nov 11, 2020

same issue happened with me, any solution or workaround ?... this is the stack trace ...
i am using latest stable version from xamarin forms

@ahmedroshdy You are seeing this using Xamarin.Forms 4.8.0.1560?

@hartez hartez moved this from Backlog to To do (blockers) in CollectionView Nov 11, 2020
@hartez hartez removed the in-progress This issue has an associated pull request that may resolve it! label Nov 11, 2020
@hartez hartez moved this from To do (blockers) to Review Backlog in CollectionView Feb 5, 2021
@hartez hartez closed this as completed Feb 7, 2021
CollectionView automation moved this from Review Backlog to Done Feb 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/collectionview i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often m/high impact ⬛ p/iOS 🍎 t/bug 🐛
Projects
v4.6.0
  
Done
Development

No branches or pull requests