Home > Mobile >  Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) Swift on macOS appp
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) Swift on macOS appp

Time:11-08

In the following line there is a function that I call from another file.

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
     
        var result = myFunc()
        
        
       
        // Do any additional setup after loading the view.
    }

The function works successfully, but when it finishes, I get a result like this.

In AppDelegate @main Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

The code snippet that caused the crash:

                do {
                    try FileManager.default.createDirectory(at: textureURL.deletingLastPathComponent(), withIntermediateDirectories: true)
                } catch {
                    print("Couldn't create output directory")
                    print(error)
                    return false
                }

                guard let texture = textureSampler.texture else {
                    continue
                }

                let cgImage = texture.imageFromTexture()!.takeRetainedValue()
                let nsBitmap = NSBitmapImageRep(cgImage: cgImage)

                var imageType: NSBitmapImageRep.FileType
                let textureType = textureURL.pathExtension.lowercased()

                switch(textureType) {
                    case "png":
                        imageType = .png
                    case "jpg", "jpeg":
                        imageType = .jpeg
                    default:
                        print("Unknown texture type \"\(textureType)\" of texture \(textureURL.lastPathComponent)")
                        imageType = .png
                }

                if convertToPNG {
                    // override automatic selection here
                    imageType = .png
                    textureURL = textureURL.deletingPathExtension().appendingPathExtension("png")
                }

                let imageData = nsBitmap.representation(using: imageType, properties: [
                    NSBitmapImageRep.PropertyKey.compressionFactor: NSNumber(floatLiteral: 1.0)
                ])!

                do {
                    try imageData.write(to: textureURL)
                    alreadySaved.append(textureURL)
                } catch {
                    print("Couldn't save \(textureURL.lastPathComponent)")
                    return false
                }

CodePudding user response:

I found solution; in apple sandbox documentation

If the app crashes when you attempt to run it, specifically by receiving an EXC_BAD_INSTRUCTION signal, the most likely reason is that you previously ran a sandboxed app with the same bundle identifier but a different code signature. This crashing upon launch is an App Sandbox security feature that prevents one app from masquerading as another and thereby gaining access to the other app’s container.

And then change this line

let cgImage = texture.imageFromTexture()!.takeRetainedValue()

as below

let cgImage = texture.imageFromTexture()!.takeUnretainedValue()
  • Related