Metal addCompletedHandler 导致 Swift 6 (iOS) 崩溃

问题描述 投票:0回答:1

以下代码在使用 Swift 5 编译时运行良好,但在使用 Swift 6 编译时崩溃(堆栈跟踪如下)。在绘制方法中,注释掉

addCompletedHandler
行可以解决问题。我正在 iOS 18.0 上进行测试,并在模拟器和设备上看到相同的行为。这是怎么回事?

import Metal
import MetalKit
import UIKit

class ViewController: UIViewController {
  @IBOutlet var metalView: MTKView!

  private var commandQueue: MTLCommandQueue?

  override func viewDidLoad() {
    super.viewDidLoad()

    guard let device = MTLCreateSystemDefaultDevice() else {
      fatalError("expected a Metal device")
    }
    self.commandQueue = device.makeCommandQueue()

    metalView.device = device
    metalView.enableSetNeedsDisplay = true
    metalView.isPaused = true
    metalView.delegate = self
  }
}

extension ViewController: MTKViewDelegate {
  func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}

  func draw(in view: MTKView) {
    guard let commandQueue,
          let commandBuffer = commandQueue.makeCommandBuffer()
    else { return }

    commandBuffer.addCompletedHandler { _ in }  // works with Swift 5, crashes with Swift 6

    commandBuffer.commit()
  }
}

这是堆栈跟踪:

Thread 10 Queue : connection Queue (serial)
#0  0x000000010581c3f8 in _dispatch_assert_queue_fail ()
#1  0x000000010581c384 in dispatch_assert_queue ()
#2  0x00000002444c63e0 in swift_task_isCurrentExecutorImpl ()
#3  0x0000000104d71ec4 in closure #1 in ViewController.draw(in:) ()
#4  0x0000000104d71f58 in thunk for @escaping @callee_guaranteed (@guaranteed MTLCommandBuffer) -> () ()
#5  0x0000000105ef1950 in __47-[CaptureMTLCommandBuffer _preCommitWithIndex:]_block_invoke_2 ()
#6  0x00000001c50b35b0 in -[MTLToolsCommandBuffer invokeCompletedHandlers] ()
#7  0x000000019e94d444 in MTLDispatchListApply ()
#8  0x000000019e94f558 in -[_MTLCommandBuffer didCompleteWithStartTime:endTime:error:] ()
#9  0x000000019e95352c in -[_MTLCommandQueue commandBufferDidComplete:startTime:completionTime:error:] ()
#10 0x0000000226ef50b0 in handleMainConnectionReplies ()
#11 0x00000001800c9690 in _xpc_connection_call_event_handler ()
#12 0x00000001800cad90 in _xpc_connection_mach_event ()
#13 0x000000010581a86c in _dispatch_client_callout4 ()
#14 0x0000000105837950 in _dispatch_mach_msg_invoke ()
#15 0x0000000105822870 in _dispatch_lane_serial_drain ()
#16 0x0000000105838c10 in _dispatch_mach_invoke ()
#17 0x0000000105822870 in _dispatch_lane_serial_drain ()
#18 0x00000001058237b0 in _dispatch_lane_invoke ()
#19 0x00000001058301f0 in _dispatch_root_queue_drain_deferred_wlh ()
#20 0x000000010582f75c in _dispatch_workloop_worker_thread ()
#21 0x00000001050abb74 in _pthread_wqthread ()
ios swift metal swift6
1个回答
0
投票
commandBuffer.addCompletedHandler { @Sendable _ in } 

使闭包可发送可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.