Bug 60898 - System.ArgumentException: Handle must be valid.
Summary: System.ArgumentException: Handle must be valid.
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.5.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-11-28 19:32 UTC by max.szczurek
Modified: 2018-04-17 08:56 UTC (History)
6 users (show)

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 for Bug 60898 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description max.szczurek 2017-11-28 19:32:04 UTC
This is crashing my Android app 50% of the time, apparently randomly, whenever I navigate between master detail pages. I tried changing the MONO_GC_PARAMS to bridge-implementation=old, but in my diagnostic build, I still see MONO_GC_PARAMS = major=marksweep-conc. I tried adding an environment.txt file with a build action of AndroidEnvironment with the following line:


That didn't work in Windows or Mac OSX - the app crashes whether I've replaced that line with new, old, or tarjan. 

I think this has shown up periodically - I can privately provide a copy of my source code if necessary with steps to reproduce, but it's a pretty big project.

11-28 12:47:48.219 12303-12303/... E/AndroidRuntime: FATAL EXCEPTION: main
Process: ..., PID: 12303
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 <7cfbebb561c54efc9010b018c0846c7e>:0 
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (System.IntPtr jobject, System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00015] in <e975227ac8644a30bb0866117325de0d>:0 
  at Android.Support.V4.Widget.DrawerLayout.RemoveDrawerListener (Android.Support.V4.Widget.DrawerLayout+IDrawerListener listener) [0x00088] in <fc4222de5c684a9abf7cc4821baac4cd>:0 
  at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.Dispose (System.Boolean disposing) [0x000e4] in <b5b1173f4d49442b898c45e26ae39e48>:0 
  at Java.Lang.Object.Dispose () [0x00000] in <e975227ac8644a30bb0866117325de0d>:0 
  at Xamarin.Forms.Platform.Android.AppCompat.Platform+<>c__DisplayClass35_0.<SetPage>b__0 () [0x00015] in <b5b1173f4d49442b898c45e26ae39e48>:0 
  at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <e975227ac8644a30bb0866117325de0d>:0 
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <e975227ac8644a30bb0866117325de0d>:0 
    at (wrapper dynamic-method) System.Object:3ebd961c-97b4-4369-879b-c2f6c3df7470 (intptr,intptr)
    at mono.java.lang.RunnableImplementor.n_run(Native Method)
    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Comment 1 Jon Douglas [MSFT] 2017-11-28 20:48:06 UTC
Hi Max,

Thanks for your bug report!

This issue looks more related to a dispose call of DrawerLayout's listener based on the stack trace. Specifically it might be related to Xamarin.Forms invoking an object that's already disposed.

As per your observations of each bridge running into this issue, it sounds like a potential Forms or GC bug. I would guess Forms first however based on the Stack Trace.

Some initial things we will need:

1. What version of Xamarin Forms are you using?
2. What version of Support Libraries are you using?
3. Do you have a reproduction project you can attach to this bug report? Ideally if you can produce a minimal example project, that would help us most. We do not need a full project as we would then have to go about a private bug/NDA situation if your company requires one.

It will also be worth enabling GREF logging:


And viewing the lifecycle. Here's a Stack Overflow question somewhat outlining this:

Comment 2 max.szczurek 2017-11-28 22:47:11 UTC
Thanks for the speedy reply!

I was able to reproduce this in a minimalistic project (2 master detail screens with a single button on each, navigating from one to the other by altering the MainPage property in the App class).

Here are the versions:

1) Xamarin.forms
@) Support libs
Comment 3 Jon Douglas [MSFT] 2017-11-28 22:51:25 UTC
Hi Max,

Based on your github reproduuction it looks like you are using Support library version with API 26. This is typically an unsupported scenario which can lead to general "weirdness". Does this happen if you set your TargetFrameworkVersion(aka compileSdkVersion) to API 25 instead? Otherwise you should upgrade to a 26 version if compiling against Oreo.
Comment 4 Jon Douglas [MSFT] 2017-11-28 22:57:26 UTC
(In reply to Jon Douglas [MSFT] from comment #3)
> Hi Max,
> Based on your github reproduuction it looks like you are using Support
> library version with API 26. This is typically an unsupported
> scenario which can lead to general "weirdness". Does this happen if you set
> your TargetFrameworkVersion(aka compileSdkVersion) to API 25 instead?
> Otherwise you should upgrade to a 26 version if compiling against Oreo.

In other words, your support library version needs to match your compilation api level.
Comment 5 max.szczurek 2017-11-28 22:59:53 UTC
Yes - you're right about the support libs version - it is But, yes, it still crashes when compiling with API 25. (I had originally been compiling with the corresponding API when I encountered the error, and tried compiling against 26 to see if it'd have any impact.)
Comment 6 max.szczurek 2017-11-30 17:33:08 UTC
Just checking in - I see the status of this bug is still NEEDINFO - are you waiting on any further information from me?
Comment 7 Jon Douglas [MSFT] 2017-11-30 17:39:14 UTC
Hi Max,

A few quick things:

1. Please update to the latest stable version of Xamarin.Forms. You are on which is 2 months old compared to the new major 2.5 releases.

Also try the nightly build: https://blog.xamarin.com/try-the-latest-in-xamarin-forms-with-nightly-builds/

2. Ensure that your support library version matches your compilation target (v26 ideally)

Please also mirror these changes in your github repository.

Once you've done that, I will continue my investigation to determine if this is an Android or Forms issue. Feel free to comment when you've done that!
Comment 8 max.szczurek 2017-11-30 20:29:00 UTC
Thank you, Jon.

I updated my Xamarin.Forms version to, my support libraries to, and my target framework to API 26 (8.0 - Oreo). I'm still receiving the exception. My github repository is up-to-date. Let me know if I can provide any more information. 

I'm testing with a Galaxy Tab A (2016) running Android 5.1.1, if that makes any difference.
Comment 9 Jon Douglas [MSFT] 2017-12-01 17:34:18 UTC
I can CONFIRM this behavior based on the updated reproduction. I am redirecting this issue to Forms given the stack trace and initial investigation.
Comment 10 Johann Perez 2018-03-28 14:48:35 UTC
Is there any update on this issue somewhere else? 

This behavior is driving me crazy, if there's someone working on fixing it and I can help I'd like to as this is giving me a hard time on a couple of project.
Comment 11 batmaci 2018-03-30 11:25:39 UTC
Is that moved into Github? please find a solution for this. it is very crucial bug
Comment 12 Michal Dobrodenka 2018-04-17 07:23:30 UTC
My app crashes with

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

On line:


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.

Started to appear after updating Android support libraries from 23.3 to 27.1, but 25.1 is also problematic.
Comment 13 Michal Dobrodenka 2018-04-17 08:56:29 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.