Search code examples
c#androidxamarinxamarin.formsxamarin.forms.carouselview

ObjectDisposedException in CarouselViewRenderer


Sometimes our app crashes with the following exception:

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Xamarin.Forms.Platform.Android.CarouselViewRenderer'. at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00029] in <8b3b636835d84984ba4604c1f57b1983>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00000] in <8b3b636835d84984ba4604c1f57b1983>:0 at AndroidX.RecyclerView.Widget.RecyclerView.AddItemDecoration (AndroidX.RecyclerView.Widget.RecyclerView+ItemDecoration decor) [0x00031] in <2ad99d4ff8bf46979e52d9bcdac6318e>:0 at Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateItemDecoration () [0x00026] in <04c545f414d24a37af95d995791bb9a9>:0

Sadly this bug was not reproducible and only happens in production. I think it's a race condition between disposing and the UpdateItemDecoration.

There is already an existing bug on github: https://github.com/xamarin/Xamarin.Forms/issues/14316


Solution

  • This was caused by clearing the ItemsSource collection before disposing. Our solution was to set the ItemsSource of the CarouselView to null before clearing the bound collection when we already know that the current view will be disposed. Sorry i wasn't able to provide the source of our app.