dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.26k stars 1.76k forks source link

System.InvalidCastException when using a custom map handler on iOS and .NET 9. #26000

Open kaniosm opened 18 hours ago

kaniosm commented 18 hours ago

Description

After updating to .NET 9 (MAUI 9.0.10) whenever opening my customized map control while the app is still looking for the GPS location the app crashes with the below error: NativeHandle Registrar_Callbacks__.callback_1320_MapKit_MKMapViewMKMapViewDelegate_GetViewForAnnotation(IntPtr pobj, IntPtr sel, IntPtr p0, IntPtr p1, IntPtr* exception_gchandle). The same was not happening in .NET 8. This is happening only in release mode.

Steps to Reproduce

  1. Create a new .NET 9 MAUI project.
  2. Create a custom map hander with a handler to the GetViewForAnnotation.
  3. Opt-out from automatic handler disconnect since this will crash the app after opening the map page for a second time. (see https://github.com/dotnet/maui/issues/20612).
  4. Deploy the release build to a physical device.
  5. Request the user location
  6. Immediately open the map page

    Link to public reproduction project repository

No response

Version with bug

9.0.10 SR1

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.100 SR10

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

kevinxufei commented 7 hours ago

Could you provide us with a sample project so we can investigate it further? Looking forward to your reply!

eerojaaskelainen commented 4 hours ago

Might be relative to #25762.

kaniosm commented 4 hours ago

Thanks @eerojaaskelainen, It seems that the issue is indeed the same, but it's not happening all times. In my case I noticed that it always happens whenever I open the map while the user location is still running, but I faced the same under different conditions that are more difficult to replicate.

I'll try to to reproduce this in a clear project and post here. As commented on the other post, I believe it has to do with the cast of the user location to IMKAnnotation by looking at this from the original source.

// https://bugzilla.xamarin.com/show_bug.cgi?id=26416 var userLocationAnnotation = Runtime.GetNSObject(annotation.Handle) as MKUserLocation;