【发布时间】:2015-06-12 06:30:37
【问题描述】:
下面这个简单的代码在调试中工作,但在 iPhone 6 上发布失败。
XCode 6.2 (6C131e)
在最新的 64 位 iPhone 上调用 runTest 会导致 EXC_BAD_ACCESS。只有在构建优化级别设置为“更快”或更高时才会发生这种情况:-O2、-O3、-Os 或 -Ofast。所以通常在 Release 配置中。 顺便说一句,当优化级别设置为“无”或“快速”时,代码运行良好:-O0 或 -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
请解释一下为什么会这样。
【问题讨论】:
-
指令在优化过程中以复杂的方式交错。所以大概你正在发布
field(我猜你正在使用ARC)但在我们尝试向它发送bar消息之前它不是无效的财产。 (我有时会发现在 nilification 命令之后添加一个额外的 NSLog 这样简单的东西可以避免这种崩溃。)这种情况完全是人为的,所以不要那样做。如果您想做一些有用的事情,请将示例作为错误报告发送给 Apple。 -
感谢您的建议,已发布到 Apple Bug Reporter,问题 ID 20449878
标签: ios iphone xcode release iphone-64bit