我正在SwiftUI中显示一个MKMapView,将其封装在UIViewRepresentable协议中。
我认为这次事故可能与";Metal API Validation Enabled"(金属API验证启用(;
我正在运行Xcode版本13.1(13A1030d(
我的iPhone是iOS 15.1
当我运行应用程序时,地图会显示,如果我旋转手机,地图会正确旋转。但旋转两三次后,应用程序就会崩溃。堆栈跟踪的顶部显示:
队列="com.Metal.CompletionQueueDispatch",停止原因=EXC_BAD_ACCESS(代码=1,地址=0x10(
显示的错误是";线程12:EXC_BAD_ACCESS(代码=1,地址=0x10(">
我已将代码缩减为以下几行ContentView.swift:
struct ContentView: View {
var body: some View {
MapView()
}
}
MapView.swift:
struct MapView: UIViewRepresentable {
typealias UIViewType = MKMapView
// Required by UIViewRepresentable protocol
func makeUIView(context: Context) -> MKMapView {
return MKMapView()
}
// Required by UIViewRepresentable protocol
func updateUIView(_ mapView: MKMapView, context: Context) {
print("updateUIView() called")
}
}
显示下面显示的控制台输出后,我来回旋转手机几次,以使地图在纵向和横向之间旋转。转了几圈后我就崩溃了。崩溃前的控制台输出如下所示。
2021-12-07 15:24:00.879362-700 UIViewRepresentableHelloWorld〔26817:1293647〕Metal API验证已启用updateUIView((调用
堆栈跟踪如下所示:
* thread #18, queue = 'com.Metal.CompletionQueueDispatch', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
* frame #0: 0x0000000197a3a014 libobjc.A.dylib`objc_msgSend + 20
frame #1: 0x00000001cc5809a4 MetalTools`-[MTLDebugCommandBuffer preCompletionHandlers] + 316
frame #2: 0x00000001cc536070 MetalTools`-[MTLToolsCommandBuffer invokeCompletedHandlers] + 36
frame #3: 0x000000019958efa0 Metal`MTLDispatchListApply + 44
frame #4: 0x000000019958f374 Metal`-[_MTLCommandBuffer didCompleteWithStartTime:endTime:error:] + 596
frame #5: 0x00000001cb3fe144 IOGPU`-[IOGPUMetalCommandBuffer didCompleteWithStartTime:endTime:error:] + 216
frame #6: 0x000000019958f04c Metal`-[_MTLCommandQueue commandBufferDidComplete:startTime:completionTime:error:] + 132
frame #7: 0x00000001cb3fdf04 IOGPU`__IOGPUNotificationQueueSetDispatchQueue_block_invoke + 148
frame #8: 0x000000010486a048 libdispatch.dylib`_dispatch_client_callout4 + 16
frame #9: 0x0000000104885364 libdispatch.dylib`_dispatch_mach_msg_invoke + 424
frame #10: 0x0000000104870ff0 libdispatch.dylib`_dispatch_lane_serial_drain + 332
frame #11: 0x0000000104886188 libdispatch.dylib`_dispatch_mach_invoke + 504
frame #12: 0x0000000104870ff0 libdispatch.dylib`_dispatch_lane_serial_drain + 332
frame #13: 0x0000000104871e08 libdispatch.dylib`_dispatch_lane_invoke + 484
frame #14: 0x0000000104870ff0 libdispatch.dylib`_dispatch_lane_serial_drain + 332
frame #15: 0x0000000104871dd4 libdispatch.dylib`_dispatch_lane_invoke + 432
frame #16: 0x000000010487d4e8 libdispatch.dylib`_dispatch_workloop_worker_thread + 852
frame #17: 0x00000001da86de84 libsystem_pthread.dylib`_pthread_wqthread + 284
修复似乎是取消选中产品方案设置中的"Metal API验证"复选框。在Xcode中,转到";产品->方案->编辑方案…"在诊断选项卡上,取消选中"Metal API验证"复选框。