dispatch_once
将项目转换为 ARC 后,调用会导致崩溃(在模拟器中)。
我最初的问题是我EXC_BAD_ACCESS
的一个单例对象的 + (SingletonClass)shared { ... dispatch_once(..., ^{}); (在 objc_retain 调用中)崩溃了。... } 方法恰好在 dispatch_once 调用前一行。
基于日志记录和断点,我的代码没有运行到 dispatch_once 调用的块中。
我不知道原因,所以我刚刚注释掉了 dispatch_once 调用。没有那个电话,我的应用程序还没有崩溃。
之后,我尝试将 dispatch_once 放入我的应用程序之前调用的方法中。基于此,我知道 Xcode 指向恰好在 dispatch_once 调用之前的行,而不管 dispatch_once 调用的方法是什么。
对我来说主要的谜团是只有在模拟器中运行应用程序时才能重现。在设备上运行应用程序没有任何问题。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"I will crash if you won't delete the dispatch_once after me and you run me in the iOS Simulator... If you run me on a device there won't be any problem with me...");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
int a = 42;
});
return NO;
}