Cannot access a disposed object exception in xamarin forms project

Viewed 5595

I am working on a Xamarin Forms (Forms version: 2.3.4.231) project. The app works fine in iOS whereas in Android, I get random crashes. From the crash logs, it seems like its an internal crash but I don't know where to start.

I know I don't have enough information in the app. But can somebody help me to understand the issue better?

Here is the crash log:

    11/07/2017 PM 12:33:13.216: Cannot access a disposed object.
Object name: 'Xamarin.Forms.Platform.Android.FormsTextView'.  at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00030] in <b1a275385773449f855cd147e39ca2c4>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeNonvirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00001] in <b1a275385773449f855cd147e39ca2c4>:0 
  at Android.Widget.TextView.set_TextFormatted (Java.Lang.ICharSequence value) [0x00028] in <37dcd078418a4ced956136dfaeb51ee2>:0 
  at Android.Widget.TextView.set_Text (System.String value) [0x00013] in <37dcd078418a4ced956136dfaeb51ee2>:0 
  at Xamarin.Forms.Platform.Android.LabelRenderer.UpdateText () [0x00098] in <1481f85a917c4f2b882ea161e9bc082f>:0 
  at Xamarin.Forms.Platform.Android.LabelRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x000c5] in <1481f85a917c4f2b882ea161e9bc082f>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
  at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00012] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x000fb] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0015b] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x001f9] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__47_0 () [0x00000] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in <37dcd078418a4ced956136dfaeb51ee2>:0 
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <37dcd078418a4ced956136dfaeb51ee2>:0 
  at (wrapper dynamic-method) System.Object:0cbcd7f2-854c-44ac-8467-eb8b42716bc1 (intptr,intptr)
2 Answers

This seems to be an issue many are having (1, 2, 3, 4). It may be fixed in 2.3.5 so try installing one of the 2.3.5 XF libraries and see if the problem goes away.

Also there are a few answers here that may work for you.

  1. Create a Entry and/or Label renderer, override OnAttachedToWindow and Dispose and catch the exception.

  2. Try setting ListView.CachingStrategy to RecycleElement

Unfortunately we hit this bug on Xamarin.Forms 3.6.X. This may be fixed in 4.X, I don't know, but in the meantime we were able to solve it by creating our own LabelRenderer as @hvaughan3 suggested.

To save some typing, here it is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Label), typeof(Your.Namespace.Renderers.YourLabelRenderer))]
namespace Your.Namespace.Renderers
{
    public class YourLabelRenderer : LabelRenderer
    {
        public YourLabelRenderer(Android.Content.Context context) : base(context) { }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            try
            {
                base.OnElementPropertyChanged(sender, e);
            }
            catch (System.ObjectDisposedException)
            {
                //This is addressing a crash in Xamarin forums 3.6 
            }
        }
    }
}
Related