3

This simple code below works in debug, but fails in release on iPhone 6.

XCode 6.2 (6C131e)

Calling runTest causes the EXC_BAD_ACCESS on the latest 64bit iPhones. And it only happens when build Optimization Levels set 'Faster' and higher: -O2, -O3, -Os or -Ofast. So usually in Release configuration. BTW, the code works well when Optimization Levels set 'None' or 'Fast': -O0 or -O1.

#import <objc/NSObject.h>

@interface Foo : NSObject
@end

@interface Test : NSObject {
    Foo *field;
}
@end

@implementation Foo

- (Foo *)bar {
    return self;
}

@end

@implementation Test

- (void)runTest {
    Foo *foo = [[Foo alloc] init];
    field = foo;
    field = nil;
    [foo bar];
}

@end

Please explain me why this happens.

Igor Zubchenok
  • 675
  • 8
  • 18
  • 2
    Instructions get interleaved in complex ways during optimization. So presumably you are releasing `field` (I am guessing you're using ARC) but it is not nilified property before we reach the attempt to send it the `bar` message. (I sometimes find that something as simple as an extra NSLog after the nilification command can avert this kind of crash.) The situation is utterly artificial so Don't Do That. Send the example to Apple as a bug report if you want to do something useful. – matt Apr 07 '15 at 15:42
  • Thanks for advice, posted to Apple Bug Reporter, Problem ID 20449878 – Igor Zubchenok Apr 07 '15 at 16:04

1 Answers1

0

I posted problem to Apple Bug Reporter, but Apple once requested some more details and then never responded. Now the problem report has completely disappeared.

By the way this appears to work with clang, version 7.3.

Igor Zubchenok
  • 675
  • 8
  • 18