Bug 40692 - Mono throws System.NotSupportedException: Unable to activate instance of type X from native handle error
Summary: Mono throws System.NotSupportedException: Unable to activate instance of type...
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2016-04-26 03:15 UTC by adrianknight89
Modified: 2016-05-17 16:14 UTC (History)
6 users (show)

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


Attachments
Repro (213.35 KB, application/zip)
2016-04-26 03:15 UTC, adrianknight89
Details
stack trace (134.83 KB, text/plain)
2016-04-26 03:17 UTC, adrianknight89
Details


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 ANSWERED

Description adrianknight89 2016-04-26 03:15:43 UTC
Created attachment 15839 [details]
Repro

This is a nasty bug. I have seen multiple similar bugs submitted and were eventually labeled NEEDINFO or RESOLVED NOT_REPRODUCIBLE. According to http://stackoverflow.com/a/10603714, this is a leaky abstraction. Please read the answer as well as the comments on SO. As suggested, there might be a bug with an unnecessary Dispose() call or the garbage collector or something else.

I have submitted a repro (please see attachment). The listview contains many items, each of which has an image. If you scroll up and down fast like a child, eventually the app will crash. Tested this on S6 / Marshmallow. You have to constantly scroll up and down multiple times so the error could be thrown eventually. I realized that it is too much for Mono to handle many images in a ListView at the same time. If you change the listview item height from 60 units to something bigger (so that only a few items are shown simultaneously on phone surface), it becomes much more difficult to throw the error.

In my case, the error had to do with InputStreamAdapter, but I have seen it for many different class types in my actually app.

Xamarin 4.1.0.313
Xamarin.Android 6.1.0.37
VS 2015 with Update 2
Comment 1 adrianknight89 2016-04-26 03:17:49 UTC
Created attachment 15840 [details]
stack trace
Comment 2 adrianknight89 2016-04-26 03:22:19 UTC
Here are some similar bugs I found:

https://bugzilla.xamarin.com/show_bug.cgi?id=34936
https://bugzilla.xamarin.com/show_bug.cgi?id=40154
https://bugzilla.xamarin.com/show_bug.cgi?id=39189
https://bugzilla.xamarin.com/show_bug.cgi?id=40258

Looks like Xamarin has not been able to address this problem since 2012 (looking at the post date of the stackoverflow answer). I hope the repro can help you get to the root cause of the issue.

P.S. If the list view contains only text (meaning no images), I don't see the error.
Comment 3 philip 2016-04-26 15:14:41 UTC
I have the same issue with the latest alpha bits of today.
Comment 4 Jonathan Pryor 2016-04-26 16:15:30 UTC
The complexity of everything makes it hard to distinguish between "user error" (e.g. a "bad" Dispose() call) and "framework bug" (e.g. "my bad!").

Which is why we have GREF logging: https://developer.xamarin.com/guides/android/troubleshooting/troubleshooting/#Global_Reference_Messages

In the "user error" scenario, there would be "Disposing" and `-g-` messages for the instance.

In the "framework bug" scenario, the only `-g-` messages would be as part of the GC mechanism (with related `+w+` messages), and the target instance "disappears".

That said... Mono 4.4, used in Cycle 7 -- Xamarin.Android 6.1 -- had a bug in the WeakReference handling code. The result of which is that Java.Lang.Object instances could be prematurely collected, which would result in the MissingMethodException of Attachment #15840 [details].

Good news: we found that it was a WeakReference bug and fixed it.

Bad news: the fix isn't out yet. It will be out in the next Cycle 7 release.

This particular bug should not be present in Cycle 6 -- Xamarin.Android 6.0 -- which is the current stable release series.

However...just because there's a MissingMethodException doesn't mean that it's due to the Cycle 7 bug. MissingMethodExceptions can still result from "user error".
Comment 5 philip 2016-04-26 16:25:59 UTC
I checked this using gref but i my case the handles in question do not exist in the rest of the log - only in the exception. So appears to be a a framework bug?
Comment 6 philip 2016-04-26 16:27:32 UTC
Forgot the log.  Both 0xbfada83c and 0x3b0efc39 don't have gref entries so this seems to be an attempt to create a new instance.

04-26 12:21:45.817 E/mono    ( 4704): Unhandled Exception:
04-26 12:21:45.817 E/mono    ( 4704): System.NotSupportedException: Unable to activate instance of type Java.Lang.Thread+RunnableImplementor from native handle 0xbfada83c (key_handle 0x3b0efc39). ---> System.MissingMethodException: No constructor found for Java.Lang.Thread+RunnableImplementor::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
04-26 12:21:45.817 E/mono    ( 4704): Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown.
04-26 12:21:45.817 E/mono    ( 4704): 
04-26 12:21:45.817 E/mono    ( 4704):   --- End of managed exception stack trace ---
04-26 12:21:45.817 E/mono    ( 4704): java.lang.Error: Java callstack:
04-26 12:21:45.817 E/mono    ( 4704): 	at mono.java.lang.RunnableImplementor.n_run(Native Method)
04-26 12:21:45.817 E/mono    ( 4704): 	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
04-26 12:21:45.817 E/mono    ( 4704): 	at android.os.Handler.handleCallback(Handler.java:739)
04-26 12:21:45.817 E/mono    ( 4704): 	at android.os.Handler.dispatchMessage(Handler.java:95)
04-26 12:21:45.817 E/mono    ( 4704): 	at android.os.Looper.loop(Looper.java:135)
04-26 12:21:45.817 E/mono    ( 4704): 	at android.app.ActivityThread.main(ActivityThread.java:5254)
04-26 12:21:45.817 E/mono    ( 4704): 	at java.lang.reflect.Method.invoke(Native Method)
04-26 12:21:45.817 E/mono    ( 4704): 	at java.lang.reflect.Method.invoke(Method.java:372)
04-26 12:21:45.817 E/mono    ( 4704): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-26 12:21:45.817 E/mono    ( 4704): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-26 12:21:45.817 E/mono    ( 4704): 
04-26 12:21:45.817 E/mono    ( 4704):   --- End of inner exception stack trace ---
04-26 12:21:45.817 E/mono    ( 4704):   at Java.Interop.TypeManager.CreateProxy (System.Type type, IntPtr handle, JniHandleOwnership transfer) [0x00039] in /Users/builder/data/lanes/2923/b4be22e9/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:304 
04-26 12:21:45.817 E/mono    ( 4704):   at Java.Interop.TypeManager.CreateInstance (IntPtr handle, JniHandleOwnership transfer, System.Type targetType) [0x00138] in /Users/builder/data/lanes/2923/b4be22e9/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:282 
04-26 12:21:45.817 E/mono    ( 4704):   --- End of inner exception stack trace ---
04-26 12:21:45.817 E/mono    ( 4704):   at (wrapper dynamic-method) System.Object:f8b17bc9-2a4f-4c1c-bb96-62f4451a6ba5 (intptr,intptr)
04-26 12:21:45.817 E/mono    ( 4704):   at (wrapper native-to-managed) System.Object:f8b17bc9-2a4f-4c1c-bb96-62f4451a6ba5 (intptr,intptr)
04-26 12:21:45.817 E/mono-rt ( 4704): [ERROR] FATAL UNHANDLED EXCEPTION: System.NotSupportedException: Unable to activate instance of type Java.Lang.Thread+RunnableImplementor from native handle 0xbfada83c (key_handle 0x3b0efc39). ---> System.MissingMethodException: No constructor found for Java.Lang.Thread+RunnableImplementor::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
04-26 12:21:45.817 E/mono-rt ( 4704): Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown.
04-26 12:21:45.817 E/mono-rt ( 4704): 
04-26 12:21:45.817 E/mono-rt ( 4704):   --- End of managed exception stack trace ---
04-26 12:21:45.817 E/mono-rt ( 4704): java.lang.Error: Java callstack:
04-26 12:21:45.817 E/mono-rt ( 4704): 	at mono.java.lang.RunnableImplementor.n_run(Native Method)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at android.os.Handler.handleCallback(Handler.java:739)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at android.os.Handler.dispatchMessage(Handler.java:95)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at android.os.Looper.loop(Looper.java:135)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at android.app.ActivityThread.main(ActivityThread.java:5254)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at java.lang.reflect.Method.invoke(Native Method)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at java.lang.reflect.Method.invoke(Method.java:372)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-26 12:21:45.817 E/mono-rt ( 4704): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-26 12:21:45.817 E/mono-rt ( 4704): 
04-26 12:21:45.817 E/mono-rt ( 4704):   --- End of inner exception stack trace ---
04-26 12:21:45.817 E/mono-rt ( 4704):   at Java.Interop.TypeManager.CreateProxy (System.Type type, IntPtr handle, JniHandleOwnership transfer) [0x00039] in /Users/builder/data/lanes/2923/b4be22e9/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:304 
04-26 12:21:45.817 E/mono-rt ( 4704):   at Java.Interop.TypeManager.CreateInstance (IntPtr handle, JniHandleOwnership transfer, System.Type targetType) [0x00138] in /Users/builder/data/lanes/2923/b4be22e9/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:282 
04-26 12:21:45.817 E/mono-rt ( 4704):   --- End of inner exception stack trace ---
04-26 12:21:45.817 E/mono-rt ( 4704):   at (wrapper dynamic-method) System.Object:f8b17bc9-2a4f-4c1c-bb96-62f4451a6ba5 (intptr,intptr)
04-26 12:21:45.817 E/mono-rt ( 4704):   at (wrapper native-to-managed) System.Object:f8b17bc9-2a4f-4c1c-bb96-62f4451a6ba5 (intptr,intptr)
04-26 12:21:45.819 I/monodroid-gref( 4704): +g+ grefc 554 gwrefc 0 obj-handle 0x5/I -> new-handle 0x201f06/G from thread 'Threadpool worker'(12)
04-26 12:21:45.820 I/monodroid-gref( 4704): -g- grefc 554 gwrefc 0 handle 0x201f06/G from thread 'Threadpool worker'(12)
04-26 12:21:45.822 I/monodroid-gref( 4704): +g+ grefc 554 gwrefc 0 obj-handle 0x100001/I -> new-handle 0x1f06/G from thread 'Threadpool worker'(12)
04-26 12:21:45.823 D/BluetoothGatt( 4704): readRssi() - device: CF:00:16:51:91:2E
04-26 12:21:45.825 I/monodroid-gref( 4704): +g+ grefc 555 gwrefc 0 obj-handle 0x200005/I -> new-handle 0x201f02/G from thread 'Threadpool worker'(13)
04-26 12:21:45.826 I/monodroid-gref( 4704): -g- grefc 555 gwrefc 0 handle 0x201f02/G from thread 'Threadpool worker'(13)
04-26 12:21:45.827 I/monodroid-gref( 4704): +g+ grefc 555 gwrefc 0 obj-handle 0x1/I -> new-handle 0x1f02/G from thread 'Threadpool worker'(13)
04-26 12:21:45.828 I/monodroid-gref( 4704): +g+ grefc 556 gwrefc 0 obj-handle 0x200005/I -> new-handle 0x201efe/G from thread 'Threadpool worker'(13)
04-26 12:21:45.830 I/monodroid-gref( 4704): -g- grefc 556 gwrefc 0 handle 0x201efe/G from thread 'Threadpool worker'(13)
referenceTable GDEF length=670 1
referenceTable GSUB length=7202 1
referenceTable GPOS length=24560 1
referenceTable head length=54 1
referenceTable GDEF length=22 1
referenceTable GSUB length=1206 1
referenceTable GPOS length=12 1
Comment 7 philip 2016-04-26 16:28:07 UTC
Seems to me this bug has to be reopened
Comment 8 Jonathan Pryor 2016-04-26 22:34:07 UTC
What I forgot to mention in Comment #4 is that the fix hasn't been released yet; Xamarin.Android 6.1.0.37 is broken.

The next release will contain the fix.

I'm sorry for the inconvenience.
Comment 9 Kasper 2016-05-17 15:27:08 UTC
I have this issue in Cycle 8 as well (aka 6.1.99.244). Is this to be expected? I see you are still using Mono 4.4.0.
Comment 10 philip 2016-05-17 16:14:03 UTC
For me this was indeed a bug in Xamarin.Android 6.1.0.37 and it went away with the next release.