Bug 51478 - System.ArgumentException: Handle must be valid. Parameter name: instance on master Detail
Summary: System.ArgumentException: Handle must be valid. Parameter name: instance on m...
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-01-12 11:35 UTC by hassan tariq
Modified: 2018-04-17 08:56 UTC (History)
11 users (show)

Tags: bb
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Description hassan tariq 2017-01-12 11:35:19 UTC
This issue is occurring on master detail page Renderer. I have gone through the Xamarin forms repository and found that this issue is occurring in Master Detail page Renderer of android on line 258. Where there is 10 seconds delay added and closing the drawables.

Lines are 

await Task.Delay(100);
CloseDrawer(_masterLayout);



Use Case:
Only Tablet Specifically:
Use the master detail page in Landscape mode using the master detail page behavior as 

MasterBehavior = MasterBehavior.SplitOnLandscape;

Then switch to portrait mode while switching assign the new master detail page to Main page of the application as 

App.Current.MainPage = mainPage;

here mainPage is Master detail page.  


Error logged:

[MonoDroid] System.ArgumentException: Handle must be valid.
[MonoDroid] Parameter name: instance
[MonoDroid]   at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00017] in /Users/builder/data/lanes/4009/0a4ab557/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12069 
[MonoDroid]   at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (System.IntPtr jobject, System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/4009/0a4ab557/source/monodroid/src/Mono.Android/JNIEnv.g.cs:444 
[MonoDroid]   at Android.Support.V4.Widget.DrawerLayout.CloseDrawer (Android.Views.View drawerView) [0x00091] in <27c17fe440cf491ba8255bcefade6e02>:0 
[MonoDroid]   at Xamarin.Forms.Platform.Android.AppCompat.MasterDetailPageRenderer+<DeviceInfoPropertyChanged>d__47.MoveNext () [0x000be] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\MasterDetailPageRenderer.cs:258 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/0a4ab557/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
[MonoDroid]   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in /Users/builder/data/lanes/4009/0a4ab557/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 
[MonoDroid]   at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/4009/0a4ab557/source/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:18 
[MonoDroid]   at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/4009/0a4ab557/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36 
[MonoDroid]   at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /Users/builder/data/lanes/4009/0a4ab557/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Lang.IRunnable.cs:81
Comment 1 hassan tariq 2017-01-12 11:37:03 UTC
Here is the file link of MasterDetailPageRenderer on Repo
https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/AppCompat/MasterDetailPageRenderer.cs
Comment 2 Waqas Yousuf 2017-05-10 19:31:25 UTC
I am having the same issue. would you please resolve it?
Comment 3 Tom Opgenorth 2017-06-27 17:35:28 UTC
Thank you for taking the time to submit the bug. We are unable to reproduce this issue. Please attach a reproduction to the bug by starting with a clean Xamarin.Android project and adding just the code necessary to demonstrate the issue.
Comment 4 Namyslaw Szymaniuk 2017-07-06 08:51:38 UTC
Same issue, with using Telerik RadListView, but the core of issue is most probably the same - OOTB Xamarin Forms:

Xamarin caused by: android.runtime.JavaProxyThrowable: System.ArgumentException: Handle must be valid.
Parameter name: instance
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00009] in <d0e22e84ac1447e280301d7b90743268>:0 
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (System.IntPtr jobject, System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00015] in <ded6aa63c9f342a3bc71c1aaf2ee5c87>:0 
  at Com.Telerik.Widget.List.RadListView.RemoveBehavior (Com.Telerik.Widget.List.ListViewBehavior p0) [0x00096] in <460da158265f4a9ab30bfc20f66f4cef>:0 
  at Telerik.XamarinForms.DataControlsRenderer.Android.ListViewRenderer.ClearBehaviors (Com.Telerik.Widget.List.RadListView oldControl) [0x00029] in <c7cf335427f34b76a978796372f68e8d>:0 
  at Telerik.XamarinForms.DataControlsRenderer.Android.ListViewRenderer.OnElementDetached (Telerik.XamarinForms.DataControls.RadListView oldElement) [0x0000c] in <c7cf335427f34b76a978796372f68e8d>:0 
  at Telerik.XamarinForms.Common.Android.AndroidRendererBase`2[S,T].Dispose (System.Boolean disposing) [0x00023] in <f447797ad1b04dc48aa8741bf7f44749>:0 
  at Java.Lang.Object.Finalize () [0x00048] in <ded6aa63c9f342a3bc71c1aaf2ee5c87>:0
Comment 5 Namyslaw Szymaniuk 2017-07-12 09:12:56 UTC
It seems to me, that using this "workaround" -
 https://bugzilla.xamarin.com/show_bug.cgi?id=47577 - adding *MONO_GC_PARAMS=bridge-implementation=old* flag to build, seems to work - I'm hardly trying to reproduce the issue from 1 hour, and completelly not able to do this.
Is the *TARJAN* affected? XF guys, that's sound really fcked up core, again.
Comment 6 Namyslaw Szymaniuk 2017-07-14 11:17:16 UTC
Nope, *MONO_GC_PARAMS=bridge-implementation=old* isn't a solution. I was ONCE able to reproduce exact same stacktrace on *MONO_GC_PARAMS=bridge-implementation=old*.
It's really hard to be reproduced, but I think, that this stacktrace could be good first point for you, XF tech support.

Also another stacktrace, 99% related to it:

Xamarin caused by: android.runtime.JavaProxyThrowable: System.ArgumentException: Handle must be valid.
Parameter name: instance
Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualObjectMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method)<bd30a18775d94dc8b6263aecd1ca9077>:0
Android.Runtime.JNIEnv.CallNonvirtualObjectMethod(IntPtr jobject, IntPtr jclass, IntPtr jmethod)<d855bac285f44dda8a0d8510b679b1e2>:0
Android.Support.V7.Widget.RecyclerView.GetAdapter()<00ef854e109241db9d853f1dd4d69cd9>:0
Telerik.XamarinForms.DataControlsRenderer.Android.ListViewAdapter.UpdateItemsSource(RadListView sourceOwner, RadListView targetOwner)<c7cf335427f34b76a978796372f68e8d>:0
Telerik.XamarinForms.DataControlsRenderer.Android.ListViewAdapter.UpdateCore(RadListView sourceOwner, RadListView targetOwner, string propertyName)<c7cf335427f34b76a978796372f68e8d>:0
Telerik.XamarinForms.Common.FacadeAdapterBase<S, T>.Update(object sourceOwner, object targetOwner, string propertyName, object sourceOwnerRoot, object targetOwnerRoot)<f447797ad1b04dc48aa8741bf7f44749>:0
at Telerik.XamarinForms.Common.XamarinToNativeControlExtensions.Update[T,K] (T nativeElement, K xfЕlement, System.String propertyName, System.Object sourceOwnerRoot, System.Object targetOwnerRoot) [0x00031] in <f447797ad1b04dc48aa8741bf7f44749>:0
Telerik.XamarinForms.DataControlsRenderer.Android.ListViewRenderer.OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)<c7cf335427f34b76a978796372f68e8d>:0
<Module>.invoke_void_object_PropertyChangedEventArgs(object, PropertyChangedEventArgs)(wrapper delegate-invoke)
Xamarin.Forms.BindableObject.OnPropertyChanged(string propertyName)
Xamarin.Forms.Element.OnPropertyChanged(string propertyName)
Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindableObject.BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes, bool silent)
Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, object value, SetValueFlags attributes, BindableObject.SetValuePrivateFlags privateAttributes)
Xamarin.Forms.BindingExpression.ApplyCore(object sourceObject, BindableObject target, BindableProperty property, bool fromTarget)
Xamarin.Forms.BindingExpression.Apply(bool fromTarget)
Xamarin.Forms.BindingExpression.BindingExpressionPart.<PropertyChanged>b__47_0()
Java.Lang.Thread.RunnableImplementor.Run()<d855bac285f44dda8a0d8510b679b1e2>:0
Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)<d855bac285f44dda8a0d8510b679b1e2>:0
at (wrapper dynamic-method) System.Object:4865c689-d64f-493f-9b7b-498898c8b0e2 (intptr,intptr)
mono.java.lang.RunnableImplementor.n_run(Native Method)
mono.java.lang.RunnableImplementor.run()RunnableImplementor.java:30
android.os.Handler.handleCallback()Handler.java:751
android.os.Handler.dispatchMessage()Handler.java:95
android.os.Looper.loop()Looper.java:154
android.app.ActivityThread.main()ActivityThread.java:6186
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:889
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:779
Comment 7 Eric Liu 2017-09-26 18:43:26 UTC
This exception seems like only occurs when you are using / calling
"async void" 

I saw " async void DeviceInfoPropertyChanged(object sender, PropertyChangedEventArgs e) " in the comment section


My app gets this exception only at my " public async void PinPropertyChanged(object sender, PropertyChangedEventArgs arg "


So basically, try to avoid "async void" in any IJavaObject (If your object aint inheriting javaobject then pretty much you will be fine).
I've no crash since I'm using  task.factory + begininvokeinmeainthread together inside non-async "void" (with tarjan GC).
Comment 8 Eric Liu 2017-09-26 21:46:29 UTC
< for my early comment
after 3 hour of test, I got the exception with tarjan+ with new GC too
the behaviour is always happening when I kinda overload the GC (memory first ofc)

09-26 21:35:16.816 D/Mono    ( 5866): GC_BRIDGE waiting for bridge processing to finish
09-26 21:35:16.816 D/Mono    ( 5866): GC_BRIDGE waiting for bridge processing to finish
09-26 21:35:16.819 I/art     ( 5866): Waiting for a blocking GC Explicit
09-26 21:35:16.825 I/art     ( 5866): WaitForGcToComplete blocked for 5.634ms for cause Explicit
09-26 21:35:16.825 I/art     ( 5866): Starting a blocking GC Explicit
09-26 21:35:16.849 I/art     ( 5866): Explicit concurrent mark sweep GC freed 12893(916KB) AllocSpace objects, 39(1620KB) LOS objects, 23% free, 12MB/16MB, paused 896us total 23.855ms
09-26 21:35:16.851 D/Mono    ( 5866): GC_NEW_BRIDGE num-objects 1060 num_hash_entries 1107 sccs size 1107 init 0.00ms df1 0.31ms sort 0.12ms dfs2 0.81ms setup-cb 0.23ms free-data 0.88ms links 93/93/93/2 dfs passes 2260/1200 ignored 47
09-26 21:35:16.851 D/Mono    ( 5866): GC_BRIDGE: Complete, was running for 38.18ms
09-26 21:35:16.851 D/Mono    ( 5866): GC_MINOR: (Nursery full) time 2.68ms, stw 3.80ms promoted 315K major size: 4960K in use: 3750K los size: 14336K in use: 10854K




then

09-26 21:35:33.874 D/Mono    ( 5866): GC_BRIDGE waiting for bridge processing to finish
09-26 21:35:33.883 I/art     ( 5866): Waiting for a blocking GC Explicit
09-26 21:35:33.891 I/art     ( 5866): WaitForGcToComplete blocked for 8.870ms for cause Explicit
09-26 21:35:33.892 I/art     ( 5866): Starting a blocking GC Explicit
09-26 21:35:33.923 I/art     ( 5866): Explicit concurrent mark sweep GC freed 5274(839KB) AllocSpace objects, 11(936KB) LOS objects, 20% free, 15MB/19MB, paused 846us total 29.458ms
09-26 21:35:33.926 D/Mono    ( 5866): GC_NEW_BRIDGE num-objects 1557 num_hash_entries 1593 sccs size 1593 init 0.00ms df1 0.83ms sort 0.81ms dfs2 0.78ms setup-cb 0.31ms free-data 1.56ms links 69/69/64/2 dfs passes 3225/1662 ignored 56
09-26 21:35:33.926 D/Mono    ( 5866): GC_BRIDGE: Complete, was running for 50.52ms
09-26 21:35:33.926 D/Mono    ( 5866): GC_MAJOR: (LOS overflow) time 11.11ms, stw 15.09ms los size: 10240K in use: 604K
09-26 21:35:33.926 D/Mono    ( 5866): GC_MAJOR_SWEEP: major size: 4976K in use: 3525K


before the next GC blocking is happenning (I mean it should), the exception come up
Comment 9 Paul DiPietro [MSFT] 2017-10-31 13:45:18 UTC
As we have have not received a reproduction, please reopen this with an attached project if this is reproducible on the latest stable/prerelease builds of Forms. We also always welcome pull requests from the community if you believe you have a fix for the issue in question. Thanks!
Comment 10 Michal Dobrodenka 2018-04-17 07:34:38 UTC
I have this bug also.

100% repro in my project after ugrading Android support libraries from 23.3 to 27.1.

System.ArgumentException: Handle must be valid.
Parameter name: instance

On line:

activity?.RunOnUiThread(NotifyDataSetChanged);

When navigating to a new Activity with RecyclerView, this activity has started showing, not shown yet and there is event to NotifyDataSetChanged from data model.
Comment 11 Michal Dobrodenka 2018-04-17 08:56:37 UTC
Sorry for mystification, it was caused by calling manual Dispose on Adapter when setting new adapter to RecyclerView.

Adapter instance was Disposed but not unhooked from Data Source events, and in event method was code working with context.