7.3 C
Canberra
Friday, July 25, 2025

Metallic addCompletedHandler causes crash with Swift 6 (iOS)


The next code runs superb when compiled with Swift 5, however crashes when compiled with Swift 6 (stack hint under). Within the draw technique, commenting out the addCompletedHandler line fixes the issue. I am testing on iOS 18.0 and see the identical habits in each the simulator and on a tool. What is going on on right here?

import Metallic
import MetalKit
import UIKit

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

  non-public var commandQueue: MTLCommandQueue?

  override func viewDidLoad() {
    tremendous.viewDidLoad()

    guard let system = MTLCreateSystemDefaultDevice() else {
      fatalError("anticipated a Metallic system")
    }
    self.commandQueue = system.makeCommandQueue()

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

extension ViewController: MTKViewDelegate {
  func mtkView(_ view: MTKView, drawableSizeWillChange dimension: 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()
  }
}

Here is the stack hint:

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 (@assured 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 ()

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles