Skip to content

ArgumentOutOfRangeException in MAUI app #3703

@Jean-RobertLaffineur

Description

@Jean-RobertLaffineur

What happened?

I have a .Net 8 MAUI app that displays Realm objects in a CollectionView, using standard MVVM XAML. This CollectionView is populated from a Realm query and objects are added / updated / removed.

I often encounter an ArgumentOutOfRangeException when the page containing the CollectionView is displayed, but this seems to only occur in Release on a Windows device (Surface tablet), and never in Debug from Visual Studio.

The exception cal stack is:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'index')
   at System.Collections.ObjectModel.Collection`1.Insert(Int32 index, T item)
   at Microsoft.Maui.Controls.Platform.ObservableItemTemplateCollection.Add(NotifyCollectionChangedEventArgs args)
   at Microsoft.Maui.Controls.Platform.ObservableItemTemplateCollection.InnerCollectionChanged(NotifyCollectionChangedEventArgs args)
   at Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(IDispatcher dispatcher, Action action)
   at Microsoft.Maui.Controls.WeakNotifyCollectionChangedProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at Realms.RealmCollectionBase`1.OnChange(IRealmCollection`1 sender, ChangeSet change)
   at Realms.NotificationCallbacks`1.Notify(ChangeSet changes, KeyPathsCollectionType kpcType)
   at Realms.RealmCollectionBase`1.Realms.INotifiable<Realms.NotifiableObjectHandleBase.CollectionChangeSet>.NotifyCallbacks(Nullable`1 changes, KeyPathsCollectionType type, Delegate callback)
   at Realms.NotifiableObjectHandleBase.NotifyObjectChanged(IntPtr managedHandle, CollectionChangeSet* changes, KeyPathsCollectionType type, IntPtr callback)
   at Realms.SynchronizationContextScheduler.scheduler_invoke_function(IntPtr function_ptr, Boolean can_execute)
   at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0()

I have not been able to isolate a reproduction project yet, so my questions are:

  1. Is there some typical mistake, in terms of synchronization or thread context, that could potentially cause the above issue? I guess that the invalid index above could be due to the Realm notification being invalidated (the collection having changed) before it reaches the MAUI control where it is consumed?
  2. Is there any logging I can enable to help diagnosing the issue?

I am using the latest (20.1.0) Realm and (8.0.93) MAUI libraries.

Thanks.

Repro steps

No precise scenario yet

Version

8

What Atlas Services are you using?

Local Database only

What type of application is this?

Other

Client OS and version

Windows 11

Code snippets

No response

Stacktrace of the exception/crash you're getting

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions