Bug 42505 - System.NotSupportedException: Unable to activate instance of type MonoDroid.MergeAdapter.MergeAdapter
Summary: System.NotSupportedException: Unable to activate instance of type MonoDroid.M...
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 6.1.0 (C7)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2016-07-13 09:31 UTC by Jakub Holovsky
Modified: 2017-06-28 15:38 UTC (History)
2 users (show)

Tags:
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 Jakub Holovsky 2016-07-13 09:31:19 UTC
Following exception occurred for our app:

Android: 6.0.1
Manufacturer: samsung
Model: SM-N9208
Date: Wed Jul 13 00:26:49 GMT+08:00 2016

java.lang.Throwable: System.NotSupportedException: Unable to activate instance of type MonoDroid.MergeAdapter.MergeAdapter from native handle 0x7ff40450d4 (key_handle 0x9001e95). ---> System.MissingMethodException: No constructor found for MonoDroid.MergeAdapter.MergeAdapter::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown.

  --- End of managed exception stack trace ---
java.lang.Error: Java callstack:
	at md5dbc392097d1132e0477cbfb39fd7e7a1.MergeAdapter.n_getCount(Native Method)
	at md5dbc392097d1132e0477cbfb39fd7e7a1.MergeAdapter.getCount(MergeAdapter.java:48)
	at android.widget.AdapterView.isScrollableForAccessibility(AdapterView.java:1243)
	at android.widget.AdapterView.onInitializeAccessibilityNodeInfoInternal(AdapterView.java:1218)
	at android.widget.AbsListView.onInitializeAccessibilityNodeInfoInternal(AbsListView.java:1988)
	at android.widget.ListView.onInitializeAccessibilityNodeInfoInternal(ListView.java:4029)
	at android.view.View.onInitializeAccessibilityNodeInfo(View.java:6621)
	at android.view.View.createAccessibilityNodeInfoInternal(View.java:6580)
	at android.view.View.createAccessibilityNodeInfo(View.java:6565)
	at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:145)
	at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:119)
	at android.view.View.onInitializeAccessibilityEventInternal(View.java:6517)
	at android.widget.AdapterView.onInitializeAccessibilityEventInternal(AdapterView.java:1228)
	at android.view.View.onInitializeAccessibilityEvent(View.java:6505)
	at android.widget.AdapterView.onRequestSendAccessibilityEventInternal(AdapterView.java:1200)
	at android.view.ViewGroup.onRequestSendAccessibilityEvent(ViewGroup.java:919)
	at android.view.ViewGroup.requestSendAccessibilityEvent(ViewGroup.java:892)
	at android.view.ViewGroup.requestSendAccessibilityEvent(ViewGroup.java:896)
	at android.view.ViewGroup.requestSendAccessibilityEvent(ViewGroup.java:896)
	at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:6376)
	at android.view.View.sendAccessibilityEventUnchecked(View.java:6349)
	at android.view.View.sendAccessibilityEventInternal(View.java:6326)
	at android.widget.TextView.sendAccessibilityEventInternal(TextView.java:10590)
	at android.view.View.sendAccessibilityEvent(View.java:6293)
	at android.view.View.sendAccessibilityHoverEvent(View.java:8935)
	at android.view.View.onHoverEvent(View.java:11121)
	at android.widget.TextView.onHoverEvent(TextView.java:9547)
	at android.view.View.dispatchHoverEvent(View.java:10172)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.widget.AbsListView.dispatchHoverEvent(AbsListView.java:4849)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.ViewGroup.exitHoverTargets(ViewGroup.java:1994)
	at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3357)
	at android.view.ViewRootImpl.dispatchDetachedFromWindow(ViewRootImpl.java:3708)
	at android.view.ViewRootImpl.doDie(ViewRootImpl.java:6887)
	at android.view.ViewRootImpl.die(ViewRootImpl.java:6860)
	at android.view.WindowManagerGlobal.removeViewLocked(WindowManagerGlobal.java:424)
	at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:379)
	at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:122)
	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5107)
	at android.app.ActivityThread.access$1700(ActivityThread.java:222)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1854)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:158)
	at android.app.ActivityThread.main(ActivityThread.java:7229)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

  --- End of inner exception stack trace ---
  at Java.Interop.TypeManager.CreateProxy (System.Type type, IntPtr handle, JniHandleOwnership transfer) <0x7f691db798 + 0x001e4> in <filename unknown>:0 
  at Java.Interop.TypeManager.CreateInstance (IntPtr handle, JniHandleOwnership transfer, System.Type targetType) <0x7f6f019518 + 0x002db> in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at Java.Interop.TypeManager.CreateInstance (IntPtr handle, JniHandleOwnership transfer, System.Type targetType) <0x7f6f019518 + 0x00588> in <filename unknown>:0 
  at Java.Lang.Object.GetObject (IntPtr handle, JniHandleOwnership transfer, System.Type type) <0x7f6f018c08 + 0x0033f> in <filename unknown>:0 
  at Java.Lang.Object._GetObject[T] (IntPtr handle, JniHandleOwnership transfer) <0x7f6f018a80 + 0x0004b> in <filename unknown>:0 
  at Java.Lang.Object.GetObject[T] (IntPtr handle, JniHandleOwnership transfer) <0x7f6f018838 + 0x0002b> in <filename unknown>:0 
  at Java.Lang.Object.GetObject[T] (IntPtr jnienv, IntPtr handle, JniHandleOwnership transfer) <0x7f5b856fa8 + 0x00037> in <filename unknown>:0 
  at Android.Widget.BaseAdapter.n_GetCount (IntPtr jnienv, IntPtr native__this) <0x7f50e8de88 + 0x0002b> in <filename unknown>:0 
  at (wrapper dynamic-method) System.Object:ca69dac6-1b37-4bb3-851b-e1975be16eca (intptr,intptr)
	at md5dbc392097d1132e0477cbfb39fd7e7a1.MergeAdapter.n_getCount(Native Method)
	at md5dbc392097d1132e0477cbfb39fd7e7a1.MergeAdapter.getCount(MergeAdapter.java:48)
	at android.widget.AdapterView.isScrollableForAccessibility(AdapterView.java:1243)
	at android.widget.AdapterView.onInitializeAccessibilityNodeInfoInternal(AdapterView.java:1218)
	at android.widget.AbsListView.onInitializeAccessibilityNodeInfoInternal(AbsListView.java:1988)
	at android.widget.ListView.onInitializeAccessibilityNodeInfoInternal(ListView.java:4029)
	at android.view.View.onInitializeAccessibilityNodeInfo(View.java:6621)
	at android.view.View.createAccessibilityNodeInfoInternal(View.java:6580)
	at android.view.View.createAccessibilityNodeInfo(View.java:6565)
	at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:145)
	at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:119)
	at android.view.View.onInitializeAccessibilityEventInternal(View.java:6517)
	at android.widget.AdapterView.onInitializeAccessibilityEventInternal(AdapterView.java:1228)
	at android.view.View.onInitializeAccessibilityEvent(View.java:6505)
	at android.widget.AdapterView.onRequestSendAccessibilityEventInternal(AdapterView.java:1200)
	at android.view.ViewGroup.onRequestSendAccessibilityEvent(ViewGroup.java:919)
	at android.view.ViewGroup.requestSendAccessibilityEvent(ViewGroup.java:892)
	at android.view.ViewGroup.requestSendAccessibilityEvent(ViewGroup.java:896)
	at android.view.ViewGroup.requestSendAccessibilityEvent(ViewGroup.java:896)
	at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:6376)
	at android.view.View.sendAccessibilityEventUnchecked(View.java:6349)
	at android.view.View.sendAccessibilityEventInternal(View.java:6326)
	at android.widget.TextView.sendAccessibilityEventInternal(TextView.java:10590)
	at android.view.View.sendAccessibilityEvent(View.java:6293)
	at android.view.View.sendAccessibilityHoverEvent(View.java:8935)
	at android.view.View.onHoverEvent(View.java:11121)
	at android.widget.TextView.onHoverEvent(TextView.java:9547)
	at android.view.View.dispatchHoverEvent(View.java:10172)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.widget.AbsListView.dispatchHoverEvent(AbsListView.java:4849)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.View.dispatchGenericMotionEvent(View.java:10074)
	at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:2176)
	at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1912)
	at android.view.ViewGroup.exitHoverTargets(ViewGroup.java:1994)
	at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3357)
	at android.view.ViewRootImpl.dispatchDetachedFromWindow(ViewRootImpl.java:3708)
	at android.view.ViewRootImpl.doDie(ViewRootImpl.java:6887)
	at android.view.ViewRootImpl.die(ViewRootImpl.java:6860)
	at android.view.WindowManagerGlobal.removeViewLocked(WindowManagerGlobal.java:424)
	at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:379)
	at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:122)
	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5107)
	at android.app.ActivityThread.access$1700(ActivityThread.java:222)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1854)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:158)
	at android.app.ActivityThread.main(ActivityThread.java:7229)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

My configuration:

Microsoft Visual Studio Community 2015
Version 14.0.25123.00 Update 2
Microsoft .NET Framework
Version 4.6.01038

Installed Version: Community

Visual Basic 2015   00322-20000-00000-AA253
Microsoft Visual Basic 2015

Visual C# 2015   00322-20000-00000-AA253
Microsoft Visual C# 2015

Visual C++ 2015   00322-20000-00000-AA253
Microsoft Visual C++ 2015

Windows Phone SDK 8.0 - ENU   00322-20000-00000-AA253
Windows Phone SDK 8.0 - ENU

Application Insights Tools for Visual Studio Package   5.209.60613.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2015 (RC1 Update 1)   14.1.20203.0
ASP.NET and Web Tools 2015 (RC1 Update 1)

ASP.NET Web Frameworks and Tools 2012.2   4.1.41102.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.40314.0
For additional information, visit http://www.asp.net/

Azure App Service Tools v2.8.2   14.0.20201.0
Azure App Service Tools v2.8.2

Common Azure Tools   1.7
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

DataFactoryProject   1.0
Microsoft Data Factory Package

GitHub.VisualStudio   1.0
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

JavaScript Language Service   2.0
JavaScript Language Service

JavaScript Project System   2.0
JavaScript Project System

JetBrains ReSharper Ultimate 2016.1.2    Build 105.0.20160522.80219
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2016 JetBrains, Inc.

Microsoft Azure Data Factory Node Node   1.0
Azure Data Factory extension for Visual Studio Server Explorer.

Microsoft Azure HDInsight HQL Service   2.0.2900.0
Language service for Hive query

Microsoft Azure HDInsight Tools for Visual Studio   2.0.2900.0
An integrated development environment for HDInsight application development.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

Microsoft Azure Tools   2.8
Microsoft Azure Tools for Microsoft Visual Studio 2015 - v2.8.40211.2

NuGet Package Manager   3.4.4
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   14.0.60311.1
Microsoft SQL Server Data Tools

TypeScript   1.8.31.0
TypeScript tools for Visual Studio

Visual Studio Tools for Universal Windows Apps   14.0.25219.00
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin   4.1.1.3 (34a92cd)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   6.1.1.1 (7db2aac)
Visual Studio extension to enable development for Xamarin.Android.

Xamarin.iOS   9.8.1.4 (3cf8aae)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 1 Jonathan Pryor 2016-07-13 18:39:27 UTC
Are you able to consistently repro this? Or is this a "random" report?

What's apparently happening is:

1. MonoDroid.MergeAdapter.MergeAdapter instance is created from managed code.

2. The instance created in (1) is Dispose()d or garbage collected.

    *Probably* Dispose()d? See (3).

3. The Java-side instance is still alive, and Java invokes `MergeAdapter.getCount()`.

(2) is where the problem occurs: if it didn't happen, (3) would be perfectly fine, because the MergeAdapter(IntPtr, JniHandleOwnership) constructor wouldn't be required, because there'd be a pre-existing instance mapped to the Java instance.

The question, then, is *how* is (2) failing? To answer that, we need GREF logs:

https://developer.xamarin.com/guides/android/troubleshooting/troubleshooting/#Global_Reference_Messages

To do so, run:

> $ adb shell setprop debug.mono.log gref,gc

Run your app, and get it to crash.

Next, obtain the GREF log:

> adb shell run-as @PACKAGE_NAME@ cat files/.__override__/grefs.txt > g.txt

Note: @PACKAGE_NAME@ is the package name of your app.

Once you have `g.txt` and the error message from `adb logcat`, determine the missing handle, e.g. in:

> java.lang.Throwable: System.NotSupportedException:
> Unable to activate instance of type MonoDroid.MergeAdapter.MergeAdapter
> from native handle 0x7ff40450d4 (key_handle 0x9001e95). ...

You want the value `0x9001e95`. Search for that value within `g.txt`, and try to trace the handle's lifetime to see where it was disposed.

If it's from a Dispose() call, Don't Do That™; remove the Dispose() call, or otherwise ensure that Java code won't call `MergeAdapter.getCount()` on that instance after you Dispose() of it.

If there is no Dispose() call, and it's instead collected by the GC, that suggests a Xamarin.Android bug, and we would need a repro and repro instructions.
Comment 2 Jakub Holovsky 2016-07-14 08:06:29 UTC
Hello Jonathan, thank you so much for such detailed answer. This was so far indeed just one time issue that I haven't been able to reproduce. When I saw that it occurred in the merge adapter I immediately assumed it was something wrong in Xamarin.Android. 

I checked our activities where merge adapters are being used and we do call "Dispose" on them but only in "OnDestroy". I wonder if there could be the problem. However we do keep reference to the merge adapter in a field so it just has to be disposed at the end.

If it occurs again I will contact you with further information.
Comment 3 John Miller [MSFT] 2017-06-28 15:38:35 UTC
Since we’re unable to reproduce this issue I will resolve this. If this issue is still occurring for you, please reopen this issue and attach a reproduction to the bug by starting with a clean Xamarin.Android project adding just the code necessary to demonstrate the issue.