Xamarin.forms: [Bug] CollectionView cannot access a disposed object.

Created on 18 Apr 2019  路  7Comments  路  Source: xamarin/Xamarin.Forms

Description

CollectionView throwing an exception if user back to the page using MasterDetailPage navigation (this is hard to explain, see steps to reproduce)

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SelectableItemsViewController'.

Steps to Reproduce

  1. Create MasterDetailPage, where Detail page contains a CollectionView and some button which push navigation to other page
  2. On the other page we don't want to do PopAsync(), instead of this we activate Detail page again
(Application.Current.MainPage as MasterDetailPage).Detail = new NavigationPage(new MainContentPage());

Expected Behavior

Working Detail page

Actual Behavior

Call stack:

    0xC in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:155,13   C#
    0x6 in System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0 at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1023,49   C#
    0xC in Foundation.NSAsyncSynchronizationContextDispatcher.Apply at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/Foundation/NSAction.cs:178,5    C#
    0x5C in UIKit.UIApplication.UIApplicationMain   C#
    0xB in UIKit.UIApplication.Main at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/UIKit/UIApplication.cs:79,4 C#
    0x2F in UIKit.UIApplication.Main at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/UIKit/UIApplication.cs:63,4    C#
>   0x8 in <MyApp>.iOS.Application.Main at <MyPath>\<MyApp>.iOS\Main.cs:12,13   C#

exact same code is working for Android, but not for iOS

Basic Information

  • Version with issue: 4.0.0-pre9
  • Last known good version: -
  • IDE: Visual Studio 2017 / Visual Studio for Mac 2019
  • Platform Target Frameworks:

    • iOS: 12.1
    • Android: android is working
  • Affected Devices: both simulator and physical device (5s)

collectionview iOS 馃崕 bug

Most helpful comment

@jsuvanto I found a workaround now...
Create a Renderer for CollectionView like this:
`public class CustomCollectionViewRenderer : CollectionViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
try
{
if (e.NewElement == null)
return;

            base.OnElementChanged(e);
        }
        catch(Exception)
        {
            // crash because e.NewElement is null after navigation
        }
    }
}`

The OnElementChanged is called after the navigation and the e.NewElement is null. Because of this the base method is crashing. So either make the if or try / catch (or just use both to be sure).

All 7 comments

@SUDALV92 Can you please attach a small project that demonstrates this issue? Thanks!

Xamarin bug test.zip

Steps to reproduce:

  1. Click "Login"
  2. Click "sign in"

Is there a workaround to this bug while waiting for the release?

We get the same error but in a different case. We have a Tabbar and if I navigate through the list and set the SelectedItem to null before navigating with the tabbar the error mentioned here is occuring. Anything we can do for this?

@jsuvanto I found a workaround now...
Create a Renderer for CollectionView like this:
`public class CustomCollectionViewRenderer : CollectionViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
try
{
if (e.NewElement == null)
return;

            base.OnElementChanged(e);
        }
        catch(Exception)
        {
            // crash because e.NewElement is null after navigation
        }
    }
}`

The OnElementChanged is called after the navigation and the e.NewElement is null. Because of this the base method is crashing. So either make the if or try / catch (or just use both to be sure).

I'm having this same issue. Is the fix available yet?

Also running into this issue when removing a page containing a CollectionView from a TabbedPage's children.

Was this page helpful?
0 / 5 - 0 ratings