Bug 40428 - System.InvalidCastExceptionSpecified in IntPtr, JniHandleOwnership
Summary: System.InvalidCastExceptionSpecified in IntPtr, JniHandleOwnership
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-15 07:55 UTC by Przemysław Raciborski
Modified: 2016-05-02 16:04 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 ANSWERED

Description Przemysław Raciborski 2016-04-15 07:55:07 UTC
Xamarin.Android 6.1.0.16,Visual Studio 2015,Android 4.4 htc desire 610, alpha preview 2: cycle 7

In Xamarin.Droid 6.1.0.16 I started to get exceptions like:
System.InvalidCastExceptionSpecified cast is not valid.

  at Java.Lang.Object.ShouldReplaceMapping (System.WeakReference current, IntPtr handle) <0x5a1b8d48 + 0x0009c> in <filename unknown>:0 
  at Java.Lang.Object._RegisterInstance (IJavaObject instance, IntPtr key, IntPtr handle) <0x5897eae8 + 0x00167> in <filename unknown>:0 
  at Java.Lang.Object.RegisterInstance (IJavaObject instance, IntPtr value, JniHandleOwnership transfer, System.IntPtr& handle) <0x5897e390 + 0x0021f> in <filename unknown>:0 
  at Java.Lang.Object.SetHandle (IntPtr value, JniHandleOwnership transfer) <0x5897e318 + 0x0002f> in <filename unknown>:0 
  at Android.Views.LayoutInflater..ctor (Android.Views.LayoutInflater original, Android.Content.Context newContext) <0x58ba9b98 + 0x00247> in <filename unknown>:0 
  at MvvmCross.Binding.Droid.Views.MvxLayoutInflater..ctor (Android.Views.LayoutInflater original, Android.Content.Context newContext, MvvmCross.Binding.Droid.Views.MvxBindingVisitor bindingVisitor, Boolean cloned) <0x58ba8d58 + 0x00033> in <filename unknown>:0 
  at MvvmCross.Binding.Droid.Views.MvxLayoutInflater.CloneInContext (Android.Content.Context newContext) <0x58bbbf68 + 0x00047> in <filename unknown>:0 
  at Android.Views.LayoutInflater.n_CloneInContext_Landroid_content_Context_ (IntPtr jnienv, IntPtr native__this, IntPtr native_newContext) <0x58bbbed0 + 0x0005b> in <filename unknown>:0 
  at (wrapper dynamic-method) System.Object:bb3744ad-5e4f-4d07-9ef6-56cfd3ea4521 (intptr,intptr,intptr)

This exception is thrown for all multiple views - MvvmCross, application-defined views, native Android Menu views and so on..
Comment 1 antlinger@controllerbox.eu 2016-05-02 13:30:31 UTC
Same Problem:

Xamarin: 4.2.0.21
Android: 4.4.3
HTC-Desire

Specified cast is not valid.
  at Java.Lang.Object.ShouldReplaceMapping (System.WeakReference current, IntPtr handle) <0x5d2bd040 + 0x0009c> in <filename unknown>:0 
  at Java.Lang.Object._RegisterInstance (IJavaObject instance, IntPtr key, IntPtr handle) <0x59841e00 + 0x00167> in <filename unknown>:0 
  at Java.Lang.Object.RegisterInstance (IJavaObject instance, IntPtr value, JniHandleOwnership transfer, System.IntPtr& handle) <0x59841678 + 0x0021f> in <filename unknown>:0 
  at Java.Lang.Object.SetHandle (IntPtr value, JniHandleOwnership transfer) <0x598415f8 + 0x0002f> in <filename unknown>:0 
  at Java.Lang.Object..ctor (IntPtr handle, JniHandleOwnership transfer) <0x59841560 + 0x0005b> in <filename unknown>:0 
  at Java.Lang.String..ctor (System.String toCopy) <0x5a31d560 + 0x00027> in <filename unknown>:0 
  at Android.Widget.TextView.set_Text (System.String value) <0x5a31d4a8 + 0x00037> in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.LabelRenderer.UpdateText () <0x5a31d000 + 0x001c3> in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.LabelRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) <0x5a321c28 + 0x001c7> in <filename unknown>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
  at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) <0x5a10e020 + 0x0004f> in <filename unknown>:0 
  at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) <0x5a10de48 + 0x00027> in <filename unknown>:0 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindablePropertyContext context, System.Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) <0x5a10d8a8 + 0x0026b> in <filename unknown>:0 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) <0x5a10c078 + 0x002eb> in <filename unknown>:0 
  at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, Boolean fromTarget) <0x5a2703c8 + 0x00707> in <filename unknown>:0 
  at Xamarin.Forms.BindingExpression.Apply (Boolean fromTarget) <0x5a279f58 + 0x000a7> in <filename unknown>:0 
  at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__38_0 () <0x5bcbe018 + 0x00023> in <filename unknown>:0 
  at Java.Lang.Thread+RunnableImplementor.Run () <0x5a287678 + 0x0003f> in <filename unknown>:0 
  at Java.Lang.IRunnableInvoker.n_Run (IntPtr jnienv, IntPtr native__this) <0x5a2875d0 + 0x0003f> in <filename unknown>:0 
  at (wrapper dynamic-method) System.Object:7d63fb64-97a8-482a-a387-733c551b2a12 (intptr,intptr)
Comment 2 Przemysław Raciborski 2016-05-02 16:02:35 UTC
I am pretty sure that this bug is caused by missing IntPtr, JniHandleOwnership constructor. After we in MvvmCross have added this missing constructor problem is not shown anymore.

Can you just treat this bug as suggested improvement -> create more meaningful exception?
Comment 3 Jonathan Pryor 2016-05-02 16:04:13 UTC
> Xamarin.Android 6.1.0.16

That's your problem. :-)

Please upgrade to Xamarin.Android 6.1.0.48, which was released last Wednesday after the Evolve keynote.