crash with simple simd code in Swift

I'm struggling to get an app working on Catalina 10.15.2 using Xcode 11.3.1, Swift 5. I could manage to boil down an example of the crash for Xcode Playgrounds:


import Cocoa
import simd

var viewMatrix = double4x4(1)
var modelMatrix = double4x4(1)

let modelViewMatrix = viewMatrix * modelMatrix

var m = modelViewMatrix.transpose.inverse

print(m)


I get an


error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).


like in the full app. Any idea what's wrong and why the code it crashing? App code works fine running natively on macOS 10.14.6 and iOS 13.


Thanks!

Replies

I have similar issues, and it is definitely related to the use of the simd inverse function (both double4x4 and double3x3).

Not fixed in Xcode 11.4 beta. However, for me, it only occurs in debug-mode, not in release-mode.

Quinn has given the explanation to this random crash on Jan 15, 2020 1:41 PM.


Note that there seems to be no problem with Float.


You should file a bug, it is important that such a bug gets clear some day.

Unfortunately, also not fixed in the new xcode beta of today [Version 11.4 beta 2 (11N123k)].

Unfortunately, also not fixed in the new xcode beta of today [Version 11.4 beta 2 (11N123k)].

Indeed. However, it’s definitely worth testing with any further 11.4 betas as they are released.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I just got feedback to my bug report from Apple, saying the issue should be fixed with the latest macOS 10.15.4 beta 4. I don't have access to my Macs right now, maybe some of you want to give it a spin...

Xcode Version 11.4 beta 3 (11N132i), out today, still has this issue. I have not tried macOS 10.15.4 beta 4, but I assume it is an Xcode issue and not a macOS problem.

Hard to say, as I could not get the bug anymore !

I assume it is an Xcode issue and not a macOS problem.

It is a tools issue, but with the tools used to build the OS (-: As Fritzt reports, this is supposed to be fixed in macOS 10.15.4 beta 4. Please give it a whirl and let us know how you get along.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Indeed, fixed in macOS 10.15.4 beta 4. Awesome, thanks Apple!

Does anyone know of any other tickets or bugs related? I am also encounter something similar (though I have not been able to isolate it into a playground) around SIMD3<Float> and assignment of individual components. If I assign the entire float3 it works but assigning indivudal componenets gives me the same EXC_BAD_ACCESS (code=EXC_I386_GPFLT) error.


Like the original post, adding print statements and reordering causes it to disappear?

It’s definitely possible that bug discussed above (FB7519739 / r. 58316406) could manifest in a variety of ways. Are you still seeing this with Xcode 11.4?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yes, I am seeing with xcode 11.4 on macos 10.15.4. Unfortunately I have not been able to distill it from my macOS application to a playground. But it manifests in a very similar way (note there are images pasted here for some reason when the post is saved they do not appear but appear when I am editing them??):

Versus a different way of setting values.



Not really sure of the best way to share the project to the group.

Unfortunately I have not been able to distill it from my macOS application to a playground.

I recommend against using a playground for this sort of thing. Playgrounds have a very different compilation model than normal Swift code, which means you may never be able to reproduce this problem there.

Rather, I’d suggest one of the following:

  • Start with a new macOS project and add code to it until you trigger the problem.

  • Start with your existing project and remove code from it while the problem still reproduces.

Not really sure of the best way to share the project to the group.

If you can cut this down into a project that you’re willing to share, the best place for that is a bug report.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Bug ID: FB7658482


After some fiddling in and tearing apart the the project I have managed to distill it (using the techniques you mentioned):

- Create a new MacOS App using Storyboard

- In the ViewController.swift file place the code below

- Run the app and in the view that appears click and drag. You should get the BAD_ACCESS error.


//
//  ViewController.swift
//


import Cocoa
import simd


/* MARK: About the Bug

 This project is a segment of a larger project we have for a simple game engine with metal.
 There is a lot of matrix operations in the app so we utilize `didSet` heavily to update different
 matrices.

 In this example we have:
 - Storyboard with NSView
 - We add a pan gesture to capture the and convert it to rotation.
 - We are a "Camera" class that has a rotation property that utilizes a didSet
   This did set, in the real application, does some math for matrix updates which is not present here.

 Reproducing the Bug
 - Run the application and in the view that pops up just click and drag.


 Ways the bug disappears:
 A. Remove the 'didSet' from the camera rotation property.
 B. Instead set the rotation property as a whole, rather than the individual components.

 See the respective comments below for the code bits to uncomment/remove.

*/






class ViewController: NSViewController {
    
    let camera:Camera = Camera()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addGestureRecognizers(to: view)
    }
    
    func addGestureRecognizers(to view: NSView) {
        let pan = NSPanGestureRecognizer(target: self, action: #selector(handlePan(gesture:)))
        view.addGestureRecognizer(pan)
    }
    
    @objc func handlePan(gesture: NSPanGestureRecognizer) {
        let translation = gesture.translation(in: gesture.view)
        let delta = SIMD2<Float>(Float(translation.x),
                           Float(translation.y))
        camera.rotate(delta: delta)
        gesture.setTranslation(.zero, in: gesture.view)
    }
}




class Camera {
    var rotation: SIMD3<Float> = [0, 0, 0] {
        // A. Remove this setter to remove the EXC_BAD_ACCESS
        didSet {
            _something()
        }
    }
    func rotate(delta: SIMD2<Float>) {
        print("rotate delta: \(delta)")
        
        // B: Uncomment this, and comment the two lines under 'Bug Here' below
//        rotation = [ rotation.x + delta.x, rotation.y + delta.y, rotation.z]
        
        // Bug Here
        rotation.y += delta.x
        rotation.x += delta.y
        
    }
    
    func _something() {
        print("_something()")
    }
}