mapbox-maps-ios icon indicating copy to clipboard operation
mapbox-maps-ios copied to clipboard

Regression: EXC_BAD_ACCESS in 11.4.0 on iOS Simulator when calling `mapView.mapboxMap.cameraState`

Open capnslipp opened this issue 1 year ago • 3 comments

Environment

  • Xcode version:15.4 (15F31d)
  • iOS version: 17.5
  • Devices affected: iOS Simulator (all virtual device types)
  • Maps SDK Version: 11.4.0

Observed behavior and steps to reproduce

When calling map.mapboxMap.cameraState on SDK 11.4.0 from the main thread in an iOS Simulator build, Mapbox crashes with EXC_BAD_ACCESS.

I'm running the following code:

struct RepresentedMapboxMapView : UIViewRepresentable
{
	func makeUIView(context: Self.Context) -> MapboxMaps.MapView
	{
		// Set up the ``MapboxMaps.MapView``.
		MapboxOptions.accessToken = AccessKeys.shared.mapboxAccessToken
		let mapView = MapboxMaps.MapView(
			frame: .zero,
			mapInitOptions: .init(
				cameraOptions: .init(center: CLLocationCoordinate2D(latitude: 40.0, longitude: -100.0), zoom: 3.0),
				styleURI: .dark
			)
		)
		try! mapView.mapboxMap.setProjection(.init(name: .mercator)) // Set 2D map projection
		
		let maximumFPS = Float(UIScreen.main.maximumFramesPerSecond)
		mapView.preferredFrameRateRange = .init(minimum: maximumFPS * 2 / 3, maximum: maximumFPS, preferred: maximumFPS)
		
		runSynchronouslyOnMainThread {
			let mapboxMapCameraState = mapView.mapboxMap.cameraState
			
			// …
		}
		
		return mapView
	}
	
	func updateUIView(_ uiView: MapboxMaps.MapView, context: Context) {}
}


fileprivate func runSynchronouslyOnMainThread(_ body: () -> Void) {
	if Thread.isMainThread {
		body()
	} else {
		DispatchQueue.main.sync {
			body()
		}
	}
}

In the observed crash, this code is already running on the main thread, so the if Thread.isMainThread { body() } condition of runSynchronouslyOnMainThread is taken.

Expected behavior

Mapbox should not crash.

Notes / preliminary analysis

Issue does not occur on-device, and does not occur in SDK 11.3.0 and earlier. Turning on Address Sanitizer, Thread Sanitizer, and/or Main Thread Check in the Xcode scheme reveals no additional warnings/info (probably because Xcode cannot analyze into the SDK's release-optimized xcframeworks).

capnslipp avatar May 22 '24 21:05 capnslipp

Hey, @capnslipp thank you for the code snippet and the report! May you please also provide the crashlog? Also, does this problem affect only simluators or happens on real device too?

aleksproger avatar May 23 '24 06:05 aleksproger

Hi @aleksproger. Yes, it only occurs on iOS Simulator, never on device (that I've seen).

For the crash log… I'll try to get a simplest-usage-project together for both that and reproducibility. Will probably be sometime next week; for now we're just sticking with 11.3.0 in order to hit some deadlines.

Cheers.

capnslipp avatar May 23 '24 19:05 capnslipp

Also experiencing this bug

jeffypooo avatar Jul 03 '24 14:07 jeffypooo