Bug 44174 - MapRenderer Control Null Reference during OnCameraChange
Summary: MapRenderer Control Null Reference during OnCameraChange
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.2.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Paul DiPietro [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-09-11 22:17 UTC by Pete Schmitz
Modified: 2017-06-19 19:19 UTC (History)
6 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 Pete Schmitz 2016-09-11 22:17:33 UTC
Under a specific edge-case it appears that the Control to the Android Forms MapRenderer can be null referenced from a google map callback (OnCameraChange).

I haven't identified the exact reproduction state, however we have had a few users run into this exception with the following stack:

System.NullReferenceExceptionObject reference not set to an instance of an object

Xamarin.Forms.Maps.Android.MapRenderer.get_NativeMap()
Xamarin.Forms.Maps.Android.MapRenderer.UpdateVisibleRegion(LatLng pos)
Xamarin.Forms.Maps.Android.MapRenderer.OnCameraChange(CameraPosition pos)
Android.Gms.Maps.GoogleMap.IOnCameraChangeListenerInvoker.n_OnCameraChange_Lcom_google_android_gms_maps_model_CameraPosition_(IntPtr jnienv, IntPtr native__this, IntPtr native_position)
at (wrapper dynamic-method) System.Object:95120d73-0405-45d0-a55f-364bc24a0586 (intptr,intptr,intptr)


A quick fix could be a null conditional operator in the NativeMap getter.
Comment 1 Pete Schmitz 2016-09-11 22:25:25 UTC
I my jumped the gun here, my custom renderer is likely the source of this issue. I can simply check if the control is nonnull before attempting to access NativeMap.
Comment 2 Jan Nepras 2017-02-23 12:10:51 UTC
Having the same issue..

caused.MapRenderer(key_handle 0x8dbe48c)
--- End of inner exception stack trace ---
Java.Interop.TypeManager.CreateProxy(Type type, IntPtr handle, JniHandleOwnership transfer)<17ba3090c8e64140a45c0beb1b3a7e59>:0
Java.Interop.TypeManager.CreateInstance(IntPtr handle, JniHandleOwnership transfer, Type targetType)<17ba3090c8e64140a45c0beb1b3a7e59>:0
--- End of inner exception stack trace ---
Java.Interop.TypeManager.CreateInstance(IntPtr handle, JniHandleOwnership transfer, Type targetType)<17ba3090c8e64140a45c0beb1b3a7e59>:0
Java.Lang.Object.GetObject(IntPtr handle, JniHandleOwnership transfer, Type type)<17ba3090c8e64140a45c0beb1b3a7e59>:0
Java.Lang.Object._GetObject<T>(IntPtr handle, JniHandleOwnership transfer)<17ba3090c8e64140a45c0beb1b3a7e59>:0
Java.Lang.Object.GetObject<T>(IntPtr handle, JniHandleOwnership transfer)<17ba3090c8e64140a45c0beb1b3a7e59>:0
Java.Lang.Object.GetObject<T>(IntPtr jnienv, IntPtr handle, JniHandleOwnership transfer)<17ba3090c8e64140a45c0beb1b3a7e59>:0
Android.Gms.Maps.GoogleMap.IOnCameraChangeListenerInvoker.n_OnCameraChange_Lcom_google_android_gms_maps_model_CameraPosition_(IntPtr jnienv, IntPtr native__this, IntPtr native_position)<cede2ee4138a48bb98b62dcaee115663>:0
at (wrapper dynamic-method) System.Object:fdd683c4-4eac-47b4-b124-6b240cb63ace (intptr,intptr,intptr)
md5d956392b2b421ac2c9ccfc063253dcd9.MapRenderer.n_onCameraChange(Native Method)
md5d956392b2b421ac2c9ccfc063253dcd9.MapRenderer.onCameraChange()MapRenderer.java:55
com.google.android.gms.maps.GoogleMap$12.onCameraChange(Unknown Source)
com.google.android.gms.maps.internal.zze$zza.onTransact(Unknown Source)
android.os.Binder.transact()Binder.java:380
at yb.a(:com.google.android.gms.DynamiteModulesB:93)
maps.D.a.a(Unknown Source)
maps.D.a$1.run(Unknown Source)
android.os.Handler.handleCallback()Handler.java:739
android.os.Handler.dispatchMessage()Handler.java:95
android.os.Looper.loop()Looper.java:135
android.app.ActivityThread.main()ActivityThread.java:5343
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke()Method.java:372
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:905
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:700
Comment 3 John Hasler 2017-03-03 13:03:10 UTC
I am also having the same issue.

@Pete Schmitz did you resolve this in your custom renderer? If so can you please provide some details
Comment 4 Pete Schmitz 2017-03-03 16:15:14 UTC
Sorry, I should have updated - Despite defensive changes to our code our users still occasionally hit the original null exception.
Comment 5 Jan Nepras 2017-06-08 11:42:11 UTC
I can still see this bug in HockeyApp. Is anybody working on it?
Comment 6 James Montemagno [MSFT] 2017-06-16 15:46:30 UTC
Thank you for taking the time to submit the bug. We are unable to reproduce this issue. Please attach a reproduction to the bug by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue.

Here are some reproduction best practices: https://gist.github.com/jassmith/92405c300e54a01dcc6d
Comment 7 Pete Schmitz 2017-06-19 19:19:26 UTC
We've stopped receiving this exception once we implemented the Google Maps IOnMapReadyCallback in our map renderer (which broadcasts availability to our PCL code).

We had something similar to:

protected override void OnElementChanged(ElementChangedEventArgs<Map> e) {
    base.OnElementChanged(e);

    if (!this.isSet && Control != null && NativeMap != null) {
        this.isSet = true;
        this.extendedMap.OnMapReady();

        ....
    }
}

We switched to:


protected override void OnElementChanged(ElementChangedEventArgs<Map> e) {
    if (!this.isSet) {
        this.isSet = true;
        Control.GetMapAsync(this);
    }
}

public void OnMapReady(GoogleMap map) {
    this.NativeMap = map;
    this.NativeMap.SetOnCameraChangeListener(this);
    
    this.extendedMap.OnMapReady();

    ....
}