Swift - 除非我关闭优化,否则发布构建崩溃 [英] Swift - release build crashes unless I turn off optimization

查看:28
本文介绍了Swift - 除非我关闭优化,否则发布构建崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在 XCode 6.2 上.

This is on XCode 6.2.

如果我在发布模式下运行应用程序,它会崩溃,但关闭优化后它不会崩溃.代码看起来很简单.我已经编写 ObjC 十多年了,所以对编程等并不陌生.

If I run the app in release mode it will crash, but with optimizations off it does not crash. The code looks straightforward. I have programmed ObjC for over a decade, so not new to programming, etc.

我注意到在 32 位中它在发布模式下运行良好(最快 -O),但在 64 位真实 iOS 硬件上它会崩溃.

I note that in 32 bit it runs fine in release mode (Fastest -O), but on 64 bit real iOS hardware it crashes.

这是编译器错误吗?或者是否有可能出现仅针对某些编译器设置(这可能在 C 中发生!)而崩溃的糟糕 swift.

Is this a compiler bug ? Or is it possible to have poor swift that crashes only for some compiler settings (which can happen in C!).

我包含了代码,但我不确定它会有所帮助.

I include code, but I'm not sure that it will help.

class func attemptLogin(completionHandler: (result: JSON?, error: NSError?) -> ()) {
    // It appears that these variables are not working in the completion block in 64 with optimization on.
    let email = User.email
    let password = User.password

    // setup login.
    let parameters: [String : AnyObject] = [
        "action": "login",
        "login": [
            "email": email,
            "password": password,
            "type": User.type
        ]
    ]

    // Fire off REST POST Async
    request(.POST, baseUrl, parameters: parameters, encoding: .JSON)
        .responseSwiftyJSON { (request, response, jsonDict, error) in

                // in release mode on 64 bit, things are seriously bad here. 
            println("jsonDict login attempt: ")
            print(jsonDict)

            if let token = jsonDict["login"]["token"].string {
                println("token found is: " + token)
                User.token = token;
                User.email = email;
                User.password = password;
                completionHandler(result: jsonDict, error: nil)
            } else {
                println("No Token")
                User.token = "";
                User.email = "";
                User.password = "";

                let errorNS = NSError(domain: "stethIoUser", code: 404, userInfo: nil)

                completionHandler(result: jsonDict, error: errorNS)
            }
        }
}

推荐答案

我昨天也遇到了类似的情况.

I had a similar situation yesterday.

我运行的是 Xcode 6.2.

I was running Xcode 6.2.

如果我在发布模式下运行我的应用程序它会崩溃,但在关闭优化后它不会在发布模式下崩溃.(在调试模式下它运行良好.)

If I ran my app in Release mode it would crash, but with optimizations off it did not crash in Release mode. (In Debug mode it ran fine.)

问题?这行代码:

let parts = split(columnLetters, { $0 == "," })

是的.就是这样.split 函数根本没有拆分我的字符串.相反,它将整个原始字符串分配给parts 数组的第一个元素.毫不奇怪,这导致应用程序稍后崩溃.

Yep. That was it. The split function simply did not split my string. Instead, it assigned the entire original string to the first element of the parts array. Not surprisingly, this led to a crash later in the app.

我用这个替换了那行代码,它起作用了:

I replaced that line of code with this, and it worked:

let parts = columnLetters.componentsSeparatedByString(",")

由于以下原因,这个错误特别难以追踪:

This bug was particularly difficult to track down for the following reasons:

  1. 该应用程序在测试期间运行良好,因为它是在调试模式下编译的.消除其他因素(iOS 版本、产品与测试数据)花了一些时间才意识到崩溃仅发生在 Release 模式下.

  1. The app ran fine during testing, since it was compiled in Debug mode. It took some time to eliminate other factors (iOS version, prod vs. test data) to realize that the crash only occurred in Release mode.

如果我们关闭优化,应用会在发布模式下运行.

The app worked in Release mode if we turned off optimizations.

这次崩溃与我们两个月前部署的上一个成功发布版本以来引入的任何新代码无关.到目前为止,我们一直在使用 split 功能,没有问题.

The crash was not related to any new code introduced since our last successful Release version that we deployed two months ago. We had been using the split function without issue until now.

崩溃没有发生在有问题的代码行上.它发生在应用程序的后面,因为字符串没有被拆分.

The crash did not occur on the line of code that was the problem. It occurred later in the app, as a consequence of the string not getting split.

代码在发布模式下编译得很好.没有编译错误指向 split 函数的问题.该函数根本没有拆分字符串.

The code compiled fine in Release mode. There was no compile error that pointed to an issue with the split function. The function simply did not split the string.

我没有在 Xcode 6.3 或更高版本上测试过.(由于其他原因,我们无法升级到 Xcode 6.3/Swift 1.2,但很快就会升级.)

I have not tested this on Xcode 6.3 or later. (For other reasons, we were prevented from upgrading to Xcode 6.3 / Swift 1.2, but will upgrade soon.)

这篇关于Swift - 除非我关闭优化,否则发布构建崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆