Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

[Bug] [Crash] Unable to activate instance of type Xamarin.Essentials.MagnetometerListener #1272

Open
bohdant opened this issue May 12, 2020 · 7 comments
Labels
bug Something isn't working need-more-information Need more information to investigate a bug or proposal

Comments

@bohdant
Copy link

bohdant commented May 12, 2020

Description

Android Apps are crashing: Unable to activate instance of type Xamarin.Essentials.MagnetometerListener from native handle

Stack Trace

Fatal Exception: android.runtime.JavaProxyThrowable: System.NotSupportedException: Unable to activate instance of type Xamarin.Essentials.MagnetometerListener from native handle 0xffe2681c (key_handle 0x87af207). ---> System.MissingMethodException: No constructor found for Xamarin.Essentials.MagnetometerListener::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
--- End of inner exception stack trace ---
at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00055] in <378795aacbfc41c3b0db0fba208eeb1e>:0
at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00116] in <378795aacbfc41c3b0db0fba208eeb1e>:0
--- End of inner exception stack trace ---
at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00182] in <378795aacbfc41c3b0db0fba208eeb1e>:0
at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000c1] in <378795aacbfc41c3b0db0fba208eeb1e>:0
at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in <378795aacbfc41c3b0db0fba208eeb1e>:0
at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <378795aacbfc41c3b0db0fba208eeb1e>:0
at Java.Lang.Object.GetObject[T] (System.IntPtr jnienv, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00006] in <378795aacbfc41c3b0db0fba208eeb1e>:0
at Android.Hardware.ISensorEventListenerInvoker.n_OnSensorChanged_Landroid_hardware_SensorEvent
(System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_e) [0x00000] in <378795aacbfc41c3b0db0fba208eeb1e>:0
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.29(intptr,intptr,intptr)
at crc64a0e0a82d0db9a07d.MagnetometerListener.n_onSensorChanged(MagnetometerListener.java)
at crc64a0e0a82d0db9a07d.MagnetometerListener.onSensorChanged(MagnetometerListener.java:39)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:981)
at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7077)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

Steps to Reproduce

Can't figure out correlation, when the bug occurs

Expected Behavior

App doesn't crashes

Actual Behavior

App crashes

Basic Information

  • Version with issue: 1.5.3,1
  • Last known good version: -
  • Platform Target Frameworks:
    • Android: minSdk - 21, targetSdk-28
  • Android Support Library Version: 28.0.03
  • Nuget Packages:
  • Affected Devices: 97% devices are samsung, 3% motorolla,
    61% Android 9
    34% Android 10
    5% Android 8

Screenshots

Reproduction Link

@bohdant bohdant added the bug Something isn't working label May 12, 2020
@jamesmontemagno
Copy link
Collaborator

jamesmontemagno commented May 12, 2020

When you are using this API are you wrapping it in a try/catch? Looks like this is only on some devices, not all correct.

Not every device supports all sensors and an exception will be thrown as you can see in the logs: NotSupportedException which is expected.

@newky2k newky2k added the need-more-information Need more information to investigate a bug or proposal label May 12, 2020
@bohdant
Copy link
Author

bohdant commented May 13, 2020

When you are using this API are you wrapping it in a try/catch? Looks like this is only on some devices, not all correct.

Not every device supports all sensors and an exception will be thrown as you can see in the logs: NotSupportedException which is expected.

I will wrap it in try/catch, but we also check whether a device is supported before starting a magnetometer.

No constructor found for Xamarin.Essentials.MagnetometerListener::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)

It shouldn't say that constructor not found if device is simply not supported

@bohdant
Copy link
Author

bohdant commented May 13, 2020

When you are using this API are you wrapping it in a try/catch?

Just checked again - yes, its' wrapped in a try/catch block

@jamesmontemagno
Copy link
Collaborator

@jonathanpeppers
Copy link
Member

The way this error generally happens:

  • A C# listener has been disposed, it's gone
  • Java tries to call the listener
  • The Xamarin.Android runtime does its best to try to create the C# object and call it

Usually, it means there is some "unsubscribe" call that Java needs so it won't call again.

Looking at the code, it seems like the UnregisterListener call would be right:

Platform.SensorManager.UnregisterListener(listener, magnetometer);
listener.Dispose();
listener = null;

Does this crash only happen after leaving your Activity, backgrounding the app, or something like that?

@bohdant
Copy link
Author

bohdant commented May 14, 2020

@jonathanpeppers
I am still trying to reproduce the issue on my devices, but without success for now.
What I can see from crash reports, which we constantly receiving:
Devices states: 2% In background 0% Rooted 100% Proximity on
Devices: 95% samsung 5% motorola
OS: 51% Android 10 46% Android 9 3% Android 8

@cpraehaus
Copy link
Contributor

@bohdant Are you stopping and re-starting Magnetometer frequently? I think in this case a callback to ISensorEventListener could already be scheduled while Magnetometer.Stop() disposes the managed wrapper MagnetometerListener. The java domain then tries to activate a new instance of MagnetometerListener and fails since it does not offer the required constructor, causing the app to crash. Maybe you can try to force start/stop to see while generating sensor data to see if you can trigger the crash this way ...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working need-more-information Need more information to investigate a bug or proposal
Projects
None yet
Development

No branches or pull requests

5 participants