Cause of iOS ARC crash in optimized build XCode 6.4?

The next method fails due to the early release of ARC for InputLower, even though there is still a strong scope. This uses XCode 6.4 to build for ARM64 and only crashes when using the -Os optimizations. Is this an ARC bug, or am I doing some kind of ARC logic error?

void crasher(NSString * input) {
    NSString * inputLower = [input lowercaseString]; // should be strong ptr
    NSString * inputLower2 = inputLower; // should be a 2nd, independent strong ptr        
    int i = 1;
    while (i < 10) {
        inputLower2 = [NSString stringWithFormat:@"%@%d", inputLower, i++];
        // ERROR: inputLower is released here, so the next iteration will crash
        NSLog(@"%@", inputLower2);
    }
}

      

The crash can be avoided simply by adding a copy, but I've read the ARC rules in detail and I don't think it should be necessary:

NSString * inputLower2 = [inputLower copy];

      

If this is not my fault, I will post a bug with Apple.

+3


source to share


1 answer


I've seen this before. This is not an ARC issue, but an optimization issue, and NSLog in particular causes it. In this case, NSLog inside the while loop is causing the problem; something is being optimized. If you move NSLog after the while loop, you can see that the loop worked.



Also, I cannot reproduce the crash in Xcode 7 (so it took me so long to reproduce it, I had to switch directly to Xcode 6.4). This suggests that the Apple people are aware of this.

+2


source







All Articles