Swift - release build crashes unless I turn off optimization

前端 未结 2 1583
有刺的猬
有刺的猬 2021-01-22 17:05

This is on XCode 6.2.

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 Obj

相关标签:
2条回答
  • 2021-01-22 17:18

    I had a similar scenario. Since I believe most people coming to this question are looking more for insight on what potentially causes this, I'll share what happened with my code.

    I am using ReactiveCocoa in my project, and I set up a reactive signal to be fired anytime I had a UIView's selected stated change. It was custom view that mimics a button, but is actually derived from UIView. In this block, I was then setting all of the other button-like views in my view to have their selected states set to NO.

    Without optimizations, this was executing fine. With optimizations, it descended into an infinite loop. Weird, eh?

    In short, in my scenario, I changed this code:

    [allButtons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];
    

    to this

    for(MyButtonLikeView* button in allButtons) {
        if(button.selected) {
            button.selected = NO;
        }
    }
    

    I know this isn't likely going to be a solution to your problem. But I hope it's useful.

    In summary, use your debugger and be on the lookout for code that is correct in Debug, but may suddenly stop working in Release.

    0 讨论(0)
  • 2021-01-22 17:33

    I had a similar situation yesterday.

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

    The problem? This line of code:

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

    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. 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.

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

    3. 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.

    4. 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.

    5. 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.

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

    0 讨论(0)
提交回复
热议问题