maui icon indicating copy to clipboard operation
maui copied to clipboard

NRE in CarouselViewController on iOS 15.5 & 16.4

Open mypp74 opened this issue 8 months ago • 13 comments

Description

2025-03-21 22:38:28.494272+0100 Dopnet.YourDive.Maui[49904:537114] Unhandled managed exception: Value cannot be null. (Parameter 'key') (System.ArgumentNullException) at System.Collections.Generic.Dictionary2[[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.Maui.Controls.Handlers.Items.TemplatedCell, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].FindValue(Object key) at System.Collections.Generic.Dictionary2[[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.Maui.Controls.Handlers.Items.TemplatedCell, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].TryGetValue(Object key, TemplatedCell& value) at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.CarouselView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].UpdateTemplatedCell(TemplatedCell cell, NSIndexPath indexPath) at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.CarouselView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetCell(UICollectionView collectionView, NSIndexPath indexPath) at Microsoft.Maui.Controls.Handlers.Items.CarouselViewController.GetCell(UICollectionView collectionView, NSIndexPath indexPath) at UIKit.UICollectionView.ReloadItems(NSIndexPath[] indexPaths) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/UIKit/UICollectionView.g.cs:line 753 at Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.<>c__DisplayClass45_0.<Replace>b__0(UICollectionView c) at Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.Update(Action1 update, NotifyCollectionChangedEventArgs args) at Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.Replace(NotifyCollectionChangedEventArgs args) at Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.CollectionChanged(NotifyCollectionChangedEventArgs args) at Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.CollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.ObjectModel.ObservableCollection1[[Dopnet.YourDive.Services.DiveInfo, Dopnet.YourDive.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection1[[Dopnet.YourDive.Services.DiveInfo, Dopnet.YourDive.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnCollectionChanged(NotifyCollectionChangedAction action, Object oldItem, Object newItem, Int32 index) at System.Collections.ObjectModel.ObservableCollection1[[Dopnet.YourDive.Services.DiveInfo, Dopnet.YourDive.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetItem(Int32 index, DiveInfo item) at System.Collections.ObjectModel.Collection1[[Dopnet.YourDive.Services.DiveInfo, Dopnet.YourDive.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].set_Item(Int32 index, DiveInfo value) at Dopnet.YourDive.UI.ViewModels.DiveDetailViewModel.UpdateDive(DiveInfo dive) in C:\Users\MarcPERROT\source\repos\YourDive\Dopnet.YourDive.UI.Logic\YourDive.UI.ViewModels\DiveDetailViewModel.cs:line 614 at Dopnet.YourDive.UI.ViewModels.DiveDetailViewModel.OnDiveChanged(Object sender, ChangedEventArgs1 e) in C:\Users\MarcPERROT\source\repos\YourDive\Dopnet.YourDive.UI.Logic\YourDive.UI.ViewModels\DiveDetailViewModel.cs:line 553 at Dopnet.YourDive.UI.ViewModels.ChangedEventSource1[[Dopnet.YourDive.Services.DiveInfo, Dopnet.YourDive.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Raise(ChangedEventArgs1 e) in C:\Users\MarcPERROT\source\repos\YourDive\Dopnet.YourDive.UI.Logic\YourDive.UI.ViewModels\ChangedEventSource.cs:line 11 at Dopnet.YourDive.UI.ViewModels.IChangedEventSourceExtensions.RaiseUpdate[DiveInfo](IChangedEventSource1 source, String id, DiveInfo value) in C:\Users\MarcPERROT\source\repos\YourDive\Dopnet.YourDive.UI.Logic\YourDive.UI.ViewModels\IChangedEventSourceExtensions.cs:line 62 at Dopnet.YourDive.UI.ViewModels.IChangedEventSourceExtensions.RaiseUpdate(IChangedEventSource1 source, DiveInfo value) in C:\Users\MarcPERROT\source\repos\YourDive\Dopnet.YourDive.UI.Logic\YourDive.UI.ViewModels\IChangedEventSourceExtensions.cs:line 24 at Dopnet.YourDive.UI.ViewModels.StampsRequestViewModel.RequestStampsAsync() in C:\Users\MarcPERROT\source\repos\YourDive\Dopnet.YourDive.UI.Logic\YourDive.UI.ViewModels\StampsRequestViewModel.cs:line 97 at Dopnet.Maui.CommandFactory.<>c__DisplayClass7_0.<<CreateCommand>b__0>d.MoveNext() in C:\Users\MarcPERROT\source\repos\YourDive\Dopnet.Maui\CommandFactory.cs:line 48 --- End of stack trace from previous location --- at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) at Foundation.NSAsyncSynchronizationContextDispatcher.Apply() in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSAction.cs:line 179 --- End of stack trace from previous location --- at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 2742 at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 64 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 96 at Dopnet.YourDive.Maui.Program.Main(String[] args) in C:\Users\MarcPERROT\source\repos\YourDive\Dopnet.YourDive.Maui\Platforms\iOS\Program.cs:line 13

Steps to Reproduce

CrashCarouselView.zip

Link to public reproduction project repository

No response

Version with bug

9.0.50 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

iOS

Affected platform versions

iOS 15.5

Did you find any workaround?

No response

Relevant log output


mypp74 avatar Mar 21 '25 21:03 mypp74

I'm also having this exception, it's really unfortunate that this only happens in iOS. This used to be "Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')." and now we are getting this new exception. @PureWeen If there is any workaround please notify us or if there is a proper way for using CollectionView in iOS. I tried dispatching it on UI Thread, tried concurrency but still the exception happens.

malsabi avatar Mar 27 '25 13:03 malsabi

@malsabi Would it be possible for you to share a replication sample for this issue? I'm not sure in which scenario it occurs, making it difficult to reproduce. If you provide a sample, it would be really helpful in tracking the issue more effectively.

sheiksyedm avatar Mar 28 '25 06:03 sheiksyedm

@sheiksyedm Thank you for your reply. I couldn't replicate this issue in another repository, but It still happens in my app and I cannot share for privacy reasons. However I will provide the exception log that I'm getting maybe It can help to figure out the issue.

  at System.Collections.Generic.Dictionary2[[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.Maui.Controls.Handlers.Items.TemplatedCell, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].FindValue(Object key)
   at System.Collections.Generic.Dictionary2[[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.Maui.Controls.Handlers.Items.TemplatedCell, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].TryGetValue(Object key, TemplatedCell& value)
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].UpdateTemplatedCell(TemplatedCell cell, NSIndexPath indexPath)
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].CreateMeasurementCell(NSIndexPath indexPath)
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetPrototypeForIndexPath(NSIndexPath indexPath)
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetPrototype()
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewLayout.DetermineCellSize()
   at Microsoft.Maui.Controls.Handlers.Items.ListViewLayout.ConstrainTo(CGSize size)
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewLayout.UpdateConstraints(CGSize size)
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ConstrainItemsToBounds()
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ViewWillLayoutSubviews()
   at Microsoft.Maui.Controls.Handlers.Items.StructuredItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ViewWillLayoutSubviews()
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/5/s/xamarin-macios/src/UIKit/UIApplication.cs:line 61
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/5/s/xamarin-macios/src/UIKit/UIApplication.cs:line 96

malsabi avatar Mar 29 '25 10:03 malsabi

still present in 9.0.70 SR7

mypp74 avatar May 16 '25 05:05 mypp74

The scenario in which the bug occurs is as follows. I have a CarouselView on a first page. I navigate to a second page. On this second page, an event causes an update of the ObservableCollection linked to the CarouselView of the first page, which is therefore not in the foreground at that moment. The crash then occurs, frequently, but not systematically

mypp74 avatar May 16 '25 06:05 mypp74

The problem also exists on iPhone 14 and iOS 16.4

mypp74 avatar May 16 '25 19:05 mypp74

Here is a way to reproduce the crash. Is it possible to include this issue in the SR8?

CrashCarouselView.zip

mypp74 avatar May 16 '25 20:05 mypp74

@sheiksyedm

@malsabi Would it be possible for you to share a replication sample for this issue? I'm not sure in which scenario it occurs, making it difficult to reproduce. If you provide a sample, it would be really helpful in tracking the issue more effectively.

CrashCarouselView.zip

mypp74 avatar May 21 '25 16:05 mypp74

I am running into the same exception as well, however using a regular CollectionView in combination with a ObservableCollection when testing on a iOS Simulator with iOS 15.5. iOS 18.4 is fine though.

bnoffer avatar Jun 10 '25 10:06 bnoffer

still present in 9.0.80 SR8

mypp74 avatar Jun 13 '25 18:06 mypp74

This issue has been verified in Visual Studio Code 1.100.3 with MAUI versions (9.0.80, 9.0.70, 9.0.60, 9.0.50, 9.0.40 & 9.0.0). It can be reproduced on the iOS 15.5 platform. It works fine on the iOS 18.4 platform.

LogishaSelvarajSF4525 avatar Jun 16 '25 11:06 LogishaSelvarajSF4525

Does this issue reproduce if you use CollectionView2/CarouselView2?

https://learn.microsoft.com/en-us/dotnet/maui/whats-new/dotnet-9?view=net-maui-9.0#collectionview-and-carouselview

PureWeen avatar Jun 16 '25 14:06 PureWeen

The issue only occurs when using CarouselView 1. But for now there are other issues with CarouselView 2

mypp74 avatar Jun 16 '25 15:06 mypp74

I have similar issue with CollectionView and ObservableCollection. In my case after clearing the ObservableCollection app crashes with such exception:

 at System.Collections.Generic.Dictionary`2[[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.Maui.Controls.Handlers.Items.TemplatedCell, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].FindValue(Object key)
   at System.Collections.Generic.Dictionary`2[[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.Maui.Controls.Handlers.Items.TemplatedCell, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].TryGetValue(Object key, TemplatedCell& value)
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController`1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].UpdateTemplatedCell(TemplatedCell cell, NSIndexPath indexPath)
   at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController`1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetCell(UICollectionView collectionView, NSIndexPath indexPath)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/macios/src/UIKit/UIApplication.cs:line 69
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/macios/src/UIKit/UIApplication.cs:line 104
   at MyApp.iOS.Program.Main

I have a dirty workaround. It works for me, but for other cases it will need to be adapted locally. The idea of ​​the fix is ​​to intercept the place where an index comes that is not in the collection and return an empty object instead of null. It happens in ItemsViewController.UpdateTemplatedCell.

To fix it, you need to create your own handler for the collection view.

public class CustomCollectionViewHandler : CollectionViewHandler
{
    protected override ItemsViewController<ReorderableItemsView> CreateController(ReorderableItemsView itemsView,
        ItemsViewLayout layout)
    {
        return new CustomReorderableItemsViewController(itemsView, layout);
    }
}

public class CustomReorderableItemsViewController : ReorderableItemsViewController<ReorderableItemsView>
{
    public CustomReorderableItemsViewController(ReorderableItemsView itemsView, ItemsViewLayout layout) : base(
        itemsView,
        layout)
    {
    }

    protected override IItemsViewSource CreateItemsViewSource()
    {
        if (ItemsView.ItemsSource == null)
            return base.CreateItemsViewSource();

        return new ObservableItemsSource(ItemsView.ItemsSource, this);
    }
}

I'm using ObservableCollection as ItemSource and patched the original ObservableItemsSource

with next code:

public static readonly object EmptyItem = new object();

internal object ElementAt(int index)
{
   if (_itemsSource is IList list)
       return (index >= 0 && index < list.Count) ? list[index] : EmptyItem;

   int count = 0;
   foreach (var item in _itemsSource)
   {
       if (count == index)
           return item;
       count++;
   }

   return EmptyItem;
}

and don't forget to register the new handler in MauiProgram.CreateMauiApp

builder.ConfigureMauiHandlers(handlers =>
{
    handlers.AddHandler(typeof(CollectionView), typeof(CustomCollectionViewHandler));
});

WorkaroundFiles.zip

UserMiha avatar Jun 28 '25 20:06 UserMiha

Why its still being in the backlog?

malsabi avatar Jul 06 '25 06:07 malsabi

Indeed, we need a fix to this issue as soon as possible... CarouselView and CollectionView are one of the most common components. Does someone have any updates on this?

jaimeatsherpa avatar Jul 22 '25 08:07 jaimeatsherpa

@UserMiha I've tried your solution but I still get the same exception with a CollectionView and an ObservableCollection, can you send me a working example? Maybe I did something wrong

jaimeatsherpa avatar Jul 22 '25 08:07 jaimeatsherpa

@Ahamed-Ali Can you please also check for the "CollectionView" I'm getting similar exception and here are the logs I'm not sure if they are useful.

Unhandled managed exception: Value cannot be null. (Parameter 'key') (System.ArgumentNullException) at System.Collections.Generic.Dictionary2[[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.Maui.Controls.Handlers.Items.TemplatedCell, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].FindValue(Object key) at System.Collections.Generic.Dictionary2[[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Microsoft.Maui.Controls.Handlers.Items.TemplatedCell, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].TryGetValue(Object key, TemplatedCell& value) at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].UpdateTemplatedCell(TemplatedCell cell, NSIndexPath indexPath) at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].CreateMeasurementCell(NSIndexPath indexPath) at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetPrototypeForIndexPath(NSIndexPath indexPath) at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetPrototype() at Microsoft.Maui.Controls.Handlers.Items.ItemsViewLayout.DetermineCellSize() at Microsoft.Maui.Controls.Handlers.Items.ListViewLayout.ConstrainTo(CGSize size) at Microsoft.Maui.Controls.Handlers.Items.ItemsViewLayout.UpdateConstraints(CGSize size) at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ConstrainItemsToBounds() at Microsoft.Maui.Controls.Handlers.Items.ItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ViewWillLayoutSubviews() at Microsoft.Maui.Controls.Handlers.Items.StructuredItemsViewController1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ViewWillLayoutSubviews() at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/5/s/xamarin-macios/src/UIKit/UIApplication.cs:line 61 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/5/s/xamarin-macios/src/UIKit/UIApplication.cs:line 96

malsabi avatar Aug 05 '25 08:08 malsabi

We have the same issue on iOS. We clear an ObservableCollection and while this, we get this error.

Markus-Zoennchen avatar Sep 08 '25 11:09 Markus-Zoennchen