Description
A crash occurs when calling Platform.ClearRenderer(..) from the Dispose method block in a custom renderer.
Previously custom renderers which needed to cleanup and detach the renderer from the Element would call Platform.ClearRenderer() in the dispose method block. This worked for several years up until recently.
Steps to Reproduce
Create a custom renderer.
In the Dispose method block, call Platform.ClearRenderer(..) to detach the renderer from the Element.
App crash with stack trace below.
Expected Behavior
The app should not crash.
Actual Behavior
The app crashes with the following exception and stack trace:
System.ArgumentException: Must have a valid JNI object reference!
06-12 12:30:42.757 I/MonoDroid( 8577): UNHANDLED EXCEPTION:
06-12 12:30:42.763 I/MonoDroid( 8577): System.ArgumentException: Must have a valid JNI object reference!
06-12 12:30:42.763 I/MonoDroid( 8577): Parameter name: value
06-12 12:30:42.763 I/MonoDroid( 8577): at Android.Runtime.AndroidValueManager.RemovePeer (Java.Interop.IJavaPeerable value) [0x0001e] in <55654ebe9f2a48e6bade2862bb243f94>:0
06-12 12:30:42.763 I/MonoDroid( 8577): at Java.Interop.JniRuntime+JniValueManager.DisposePeer (Java.Interop.JniObjectReference h, Java.Interop.IJavaPeerable value) [0x0001f] in <dac4c5a4b77f4e61a5e6d9d3050dfb9f>:0
06-12 12:30:42.763 I/MonoDroid( 8577): at Java.Interop.JniRuntime+JniValueManager.DisposePeer (Java.Interop.IJavaPeerable value) [0x00038] in <dac4c5a4b77f4e61a5e6d9d3050dfb9f>:0
06-12 12:30:42.763 I/MonoDroid( 8577): at Java.Lang.Object.Dispose () [0x00000] in <55654ebe9f2a48e6bade2862bb243f94>:0
06-12 12:30:42.763 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementPackager.RemoveChild (Xamarin.Forms.VisualElement view) [0x0007f] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:275
06-12 12:30:42.763 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x000ab] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:321
06-12 12:30:42.763 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementPackager.Dispose (System.Boolean disposing) [0x000ad] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:88
06-12 12:30:42.764 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementPackager.Dispose () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:61
06-12 12:30:42.764 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Dispose (System.Boolean disposing) [0x00081] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:306
06-12 12:30:42.764 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.Platform+DefaultRenderer.Dispose (System.Boolean disposing) [0x0001a] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:1306
06-12 12:30:42.764 I/MonoDroid( 8577): at Java.Lang.Object.Java.Interop.IJavaPeerable.Disposed () [0x00000] in <55654ebe9f2a48e6bade2862bb243f94>:0
06-12 12:30:42.764 I/MonoDroid( 8577): at Java.Interop.JniRuntime+JniValueManager.DisposePeer (Java.Interop.JniObjectReference h, Java.Interop.IJavaPeerable value) [0x00019] in <dac4c5a4b77f4e61a5e6d9d3050dfb9f>:0
06-12 12:30:42.764 I/MonoDroid( 8577): at Java.Interop.JniRuntime+JniValueManager.DisposePeer (Java.Interop.IJavaPeerable value) [0x00038] in <dac4c5a4b77f4e61a5e6d9d3050dfb9f>:0
06-12 12:30:42.764 I/MonoDroid( 8577): at Java.Lang.Object.Dispose () [0x00000] in <55654ebe9f2a48e6bade2862bb243f94>:0
06-12 12:30:42.764 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementPackager.RemoveChild (Xamarin.Forms.VisualElement view) [0x0007f] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:275
06-12 12:30:42.764 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x000ab] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:321
06-12 12:30:42.764 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementPackager.Dispose (System.Boolean disposing) [0x000ad] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:88
06-12 12:30:42.765 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementPackager.Dispose () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:61
06-12 12:30:42.765 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Dispose (System.Boolean disposing) [0x00081] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:306
06-12 12:30:42.765 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.PageRenderer.Dispose (System.Boolean disposing) [0x00024] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\PageRenderer.cs:59
06-12 12:30:42.765 I/MonoDroid( 8577): at Java.Lang.Object.Java.Interop.IJavaPeerable.Disposed () [0x00000] in <55654ebe9f2a48e6bade2862bb243f94>:0
06-12 12:30:42.765 I/MonoDroid( 8577): at Java.Interop.JniRuntime+JniValueManager.DisposePeer (Java.Interop.JniObjectReference h, Java.Interop.IJavaPeerable value) [0x00019] in <dac4c5a4b77f4e61a5e6d9d3050dfb9f>:0
06-12 12:30:42.766 I/MonoDroid( 8577): at Java.Interop.JniRuntime+JniValueManager.DisposePeer (Java.Interop.IJavaPeerable value) [0x00038] in <dac4c5a4b77f4e61a5e6d9d3050dfb9f>:0
06-12 12:30:42.766 I/MonoDroid( 8577): at Java.Lang.Object.Dispose () [0x00000] in <55654ebe9f2a48e6bade2862bb243f94>:0
06-12 12:30:42.766 I/MonoDroid( 8577): at Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnDestroyView () [0x0003c] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:88
06-12 12:30:42.766 I/MonoDroid( 8577): at Android.Support.V4.App.Fragment.n_OnDestroyView (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <fefee6c2c695459088a9df092723e052>:0
06-12 12:30:42.766 I/MonoDroid( 8577): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.68(intptr,intptr) in D:\a\1\s\Xamarin.Forms.Platform.And
Basic Information
- Version with issue: 4.6.0-sr2 and above
- Last known good version: N/A - Seems related to Xamarin.Android/Mono itself.
- IDE: Microsoft Visual Studio 2019 Enterprise 16.6.2
- Platform Target Frameworks:
- Android Support Library Version: 28.0.0.3
Reproduction Link
To be supplied.
Workaround
See workaround documented below.
Description
A crash occurs when calling
Platform.ClearRenderer(..)from the Dispose method block in a custom renderer.Previously custom renderers which needed to cleanup and detach the renderer from the
Elementwould callPlatform.ClearRenderer()in the dispose method block. This worked for several years up until recently.Steps to Reproduce
Create a custom renderer.
In the Dispose method block, call
Platform.ClearRenderer(..)to detach the renderer from the Element.App crash with stack trace below.
Expected Behavior
The app should not crash.
Actual Behavior
The app crashes with the following exception and stack trace:
System.ArgumentException: Must have a valid JNI object reference!
Basic Information
Reproduction Link
To be supplied.
Workaround
See workaround documented below.