EXC_BAD_ACCESS error for invoking method in swift

青春壹個敷衍的年華 提交于 2019-12-25 04:25:01

问题


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):

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

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  

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/41018210/exc-bad-access-error-for-invoking-method-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!