简体   繁体   English

使用 LET 定义的 Swift 常量有时会在读取 (OSX) 时产生 EXC_BAD_ACCESS (EXC_I386_GPFLT)

[英]Swift constant defined with LET sometimes produces EXC_BAD_ACCESS (EXC_I386_GPFLT) on read (OSX)

In my AppDelegate I have a constant for a statusbar icon, used when the application is busy:在我的 AppDelegate 中,我有一个状态栏图标的常量,在应用程序繁忙时使用:

class AppDelegate: NSObject, NSApplicationDelegate {

let busyImage = NSImage(named: "BusyStatus");
...

In another class I'm accessing this several times over the time the app is running.在另一堂课中,我在应用程序运行期间多次访问它。 On a Macbook Air with SSD I sometimes get an在带有 SSD 的 Macbook Air 上,我有时会收到一个

EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       EXC_I386_GPFLT

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib              0x0000000103876cf9 swift_unknownRetain + 41
1   de.test.myapp   0x000000010304743b TestApp.AppDelegate.busyImage.getter : ObjectiveC.NSImage? (in TestApp) (AppDelegate.swift:0)
2   de.test.myapp   0x0000000102f9aa77 TestApp.Sync.(update (TestApp.Sync) -> () -> ()).(closure #2) (in TestApp) (Sync.swift:271)

The code there:那里的代码:

statusBarItem!.image = appDelegate!.busyImage

It's in a block called in the Main Thread:它位于主线程中调用的块中:

let updateBlock: () -> () = {

    appDelegate!.statusBarItem!.image = self.appDelegate!.busyImage
        [... other code ...]
 }

 if NSThread.isMainThread()
 {
     updateBlock()
 }
 else
 {
     dispatch_sync(dispatch_get_main_queue(),updateBlock)
 }

What can cause this and how to avoid this?什么会导致这种情况以及如何避免这种情况?

问题似乎从 swift 2 开始就消失了。再也没有这个问题并且我的代码中没有任何改变。

Hello People from the Internet,你好,来自互联网的人们,

My env is macOS 10.15.4 and I am running XCode 11.5 (11E608c) with Swift 5.我的环境是 macOS 10.15.4,我正在使用 Swift 5 运行 XCode 11.5 (11E608c)。

I have exactly the same issue with a little piece of code here which fails 100% of the time when using the rotation:我在这里的一小段代码有完全相同的问题,在使用旋转时它 100% 失败:

override func rotate(delta: float2) {
    let sensitivity: Float = 0.005
    rotation.y += delta.x * sensitivity    // FAILS with Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    _viewMatrix = updateViewMatrix()
}

This code does not look weird at all.这段代码看起来一点也不奇怪。 rotation is a vector of floats defined in the Class as: rotation是在类中定义的浮点向量:

var rotation: float3 = [0, 0, 0]

Now, I spent some time trying many versions of the same function and if I rewrite it as:现在,我花了一些时间尝试相同功能的许多版本,如果我将其重写为:

override func rotate(delta: float2) {
    let sensitivity: Float = 0.005
    var deltaX: Float = delta.x * sensitivity
    rotation.y += deltaX
    _viewMatrix = updateViewMatrix()
}

it now runs smoothly.它现在运行顺利。 This is crazy since I see no obvious reason.这很疯狂,因为我看不出明显的原因。 I do not try to change a constant and even the linter advises me to change the var to let in deltaX definition.我不会尝试更改常量,甚至deltaX建议我更改varlet deltaX定义。

I feel like this is a Swift issue but this is worrying for a 5th version of a language used by many.我觉得这是一个 Swift 问题,但这对于许多人使用的语言的第 5 版来说是令人担忧的。

I would update my answer if I find a solution or at least a valid explanation.如果我找到解决方案或至少是有效的解释,我会更新我的答案。

Live long and prosper lads!小伙子们长寿和繁荣!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM