dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.06k stars 1.73k forks source link

iOS: CollectionView Crashes when clearing ObservableCollection & adding data second time onwards #22949

Closed muffadal53 closed 3 weeks ago

muffadal53 commented 4 months ago

Description

If we have a view model with an ObservableCollection bound to a CollectionView. From the view model, we perform a Clear and then Add, it will crash out on iOS with "SIGABRT: Arg_NullReferenceException".

This happens on ".NET 8.0.300" with "Microsoft.Maui.Controls" version "8.0.40" and "Microsoft.Maui.Controls.Compatibility" with same version "8.0.40".

Below is the stack trace form the crash log:

Microsoft.Maui.Controls.Handlers.Items.GroupableItemsViewController`1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetInsetForSection(ItemsViewLayout itemsViewLayout, UICollectionView collectionView, IntPtr section)
Microsoft.Maui.Controls.Handlers.Items.GroupableItemsViewDelegator`2[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.ReorderableItemsViewController`1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, IntPtr section)
ObjCRuntime.Runtime.RethrowManagedException(IntPtr )
ObjCRuntime.Runtime.rethrow_managed_exception(IntPtr original_exception_gchandle, IntPtr* exception_gchandle)
UIKit.UICollectionViewLayout.PrepareLayout()
Microsoft.Maui.Controls.Handlers.Items.ItemsViewLayout.PrepareLayout()
ObjCRuntime.Runtime.RethrowManagedException(IntPtr )
ObjCRuntime.Runtime.rethrow_managed_exception(IntPtr original_exception_gchandle, IntPtr* exception_gchandle)
UIKit.UICollectionView.InsertItems(NSIndexPath[] )
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.<>c__DisplayClass42_0.<Add>b__0(UICollectionView c)
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.Update(Action`1 update, NotifyCollectionChangedEventArgs args)
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.Add(NotifyCollectionChangedEventArgs args)
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.CollectionChanged(NotifyCollectionChangedEventArgs args)

Steps to Reproduce

  1. Create a Collection View with IsVisible property to "IsStringNotNullOrEmptyConverter".
  2. Create and Initialize the Observable collection in ViewModel.
  3. Bind this to the collection view which is inside Grid in UI.
  4. Now, based on TextChanged event of Entry field in the UI, in code behind file invoke the ICommand on viewModel say "ValueChanged".
  5. In the "ValueChanged" command method in the ViewModel, create new list of object say Employee object and just after creating this list, call clear on the observable collection of Employees and add the list items of Employee in for loop.
  6. Notice the app crashes on iOS with stack trace mentioned in Decription and Relevant log output section.

Please provide fix for this issue as it is urent for our project release. This happens on ".NET SDK 8.0.300" with "Microsoft.Maui.Controls" version "8.0.40" and "Microsoft.Maui.Controls.Compatibility" with same version "8.0.40".

Link to public reproduction project repository

N/A

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

N/A

Relevant log output

Microsoft.Maui.Controls.Handlers.Items.GroupableItemsViewController`1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetInsetForSection(ItemsViewLayout itemsViewLayout, UICollectionView collectionView, IntPtr section)
Microsoft.Maui.Controls.Handlers.Items.GroupableItemsViewDelegator`2[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.ReorderableItemsViewController`1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, IntPtr section)
ObjCRuntime.Runtime.RethrowManagedException(IntPtr )
ObjCRuntime.Runtime.rethrow_managed_exception(IntPtr original_exception_gchandle, IntPtr* exception_gchandle)
UIKit.UICollectionViewLayout.PrepareLayout()
Microsoft.Maui.Controls.Handlers.Items.ItemsViewLayout.PrepareLayout()
ObjCRuntime.Runtime.RethrowManagedException(IntPtr )
ObjCRuntime.Runtime.rethrow_managed_exception(IntPtr original_exception_gchandle, IntPtr* exception_gchandle)
UIKit.UICollectionView.InsertItems(NSIndexPath[] )
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.<>c__DisplayClass42_0.<Add>b__0(UICollectionView c)
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.Update(Action`1 update, NotifyCollectionChangedEventArgs args)
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.Add(NotifyCollectionChangedEventArgs args)
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.CollectionChanged(NotifyCollectionChangedEventArgs args)
github-actions[bot] commented 4 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

dotnet-policy-service[bot] commented 4 months ago

Hi @muffadal53. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

drasticactions commented 4 months ago

@muffadal53 Using that version of MAUI, I could not reproduce your crash on iOS or Mac Catalyst. If you create a sample with what you're trying to implement, we can see exactly what the cause of your issue is. Thanks!

muffadal53 commented 4 months ago

@drasticactions : I have updated the steps for reproducing the issue, unfortunately I don't have iOS sample to share as it is happening in my real production application. Can you please try with updated steps an see if you are able to reproduce this isssue. The steps are as mentioned below:

  1. Create a Collection View with IsVisible property to "IsStringNotNullOrEmptyConverter" in a Content Page.

  2. Create and Initialize the Observable collection in ViewModel.

  3. Bind this to the collection view which is inside Grid in UI.

  4. Now, based on TextChanged event of Entry field in the UI, in code behind file of the Content Page invoke the ICommand on viewModel say "ValueChanged".

  5. In the "ValueChanged" command method in the ViewModel, create new list of object say Employee object and just after creating this list, call clear on the observable collection of Employees and add the list items of Employee in for loop.

  6. Notice the app crashes on iOS with stack trace mentioned in Decription and Relevant log output section.

dotnet-policy-service[bot] commented 4 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback to reproduce the issue but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.