EXC_BAD_ACCESS error for invoking method in swift

2019-09-11 07:28发布

Here is the code:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    @IBAction func changeBtn(_ sender: NSButton) {
        obj1.changeColor()
        obj2.changeBackgroundColor()
        objFld1.textColor = obj1.color
        objFld2.drawsBackground = true
        objFld2.backgroundColor = obj2.backgroundColor
    }

    @IBOutlet weak var objFld1: NSTextField!
    @IBOutlet weak var objFld2: NSTextField!
    class colored {
        var color = NSColor.black
        var backgroundColor = NSColor.gray
        func changeColor() {
            color = NSColor.yellow
        }
        func changeBackgroundColor() {
            backgroundColor = NSColor.blue
        }
    }

    var obj1: colored
    var obj2: colored
    override init() {
        obj1 = colored()
        obj2 = colored()
    }

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }


}

Everything looks innocent and compiles and starts up just fine, but when I click the "change" button, I got an error (in debug mode):

enter image description here

In release mode, the error appears at a different place:

enter image description here

Any idea about what went wrong?

macOS Sierra, Xcode 8.1 (8B62)

The debug session gives me these info:

InheritProgram`@objc AppDelegate.changeBtn(NSButton) -> ():
    0x100001f10 <+0>:  pushq  %rbp
    0x100001f11 <+1>:  movq   %rsp, %rbp
    0x100001f14 <+4>:  subq   $0x30, %rsp
    0x100001f18 <+8>:  movq   %rdi, -0x8(%rbp)
    0x100001f1c <+12>: movq   %rdx, %rdi
    0x100001f1f <+15>: movq   %rsi, -0x10(%rbp)
    0x100001f23 <+19>: movq   %rdx, -0x18(%rbp)
    0x100001f27 <+23>: callq  0x100002bd8               ; symbol stub for: objc_retain
    0x100001f2c <+28>: movq   -0x8(%rbp), %rdi
    0x100001f30 <+32>: movq   %rax, -0x20(%rbp)
    0x100001f34 <+36>: callq  0x100002bd8               ; symbol stub for: objc_retain
    0x100001f39 <+41>: movq   -0x18(%rbp), %rdi
    0x100001f3d <+45>: movq   -0x8(%rbp), %rsi
    0x100001f41 <+49>: movq   %rax, -0x28(%rbp)
    0x100001f45 <+53>: callq  0x100001bd0               ; InheritProgram.AppDelegate.changeBtn (__ObjC.NSButton) -> () at AppDelegate.swift:16
    0x100001f4a <+58>: movq   -0x8(%rbp), %rdi
    0x100001f4e <+62>: callq  0x100002bd2               ; symbol stub for: objc_release
    0x100001f53 <+67>: addq   $0x30, %rsp
    0x100001f57 <+71>: popq   %rbp
    0x100001f58 <+72>: retq  

2条回答
小情绪 Triste *
2楼-- · 2019-09-11 07:55

Still have no idea what happened there, but copying the code to a new project worked perfectly fine.

查看更多
Summer. ? 凉城
3楼-- · 2019-09-11 08:03

In a very similar situation, a Product > Clean fixed the same issue for me. A simple class was crashing with EXC_BAD_ACCESS on the first property of the file.

查看更多
登录 后发表回答