dispatch_once调用导致崩溃 [英] dispatch_once call causes crash
问题描述
dispatch_once
在我将项目转换为ARC之后,调用导致崩溃(在模拟器中)。
dispatch_once
call causes crash (in simulator) after I've converted my project to ARC.
原来的问题是,我有$ EXC_BAD_ACCESS
(在objc_retain调用)崩溃在我的一个singleton对象的+(SingletonClass)shared {... dispatch_once(..., {}); ...}方法正好在dispatch_once调用之前一行。
My original problem was that I've got EXC_BAD_ACCESS
(in objc_retain call) crash in one of my singleton object's + (SingletonClass)shared { ... dispatch_once(..., ^{}); ... } method exactly one line before the dispatch_once call.
根据loggings和断点,我的代码没有运行到dispatch_once调用的块中。
Based on loggings, and breakpoints my code have not run into the dispatch_once call's block.
我不知道原因,所以我刚刚注释掉dispatch_once调用。我的应用程序没有调用没有崩溃。
I didn't know the reason, so I've just commented out the dispatch_once call. My app haven't crashed without that call.
之后,我试图把dispatch_once在我的应用程序调用的方法。基于这一点,我知道Xcode指向正好在dispatch_once调用之前的行,而不管dispatch_once调用是什么方法。
After that I've tried to put dispatch_once in a method that my app calls earlier. Based on that I know that Xcode points to the line that is exactly before the dispatch_once call regardless of the method where the dispatch_once call is.
主要的事情是一个谜对我来说,这只是可重复的,如果我在模拟器中运行的应用程序。
The main thing that is a mystery for me is that this is only reproducible if I run the app in the simulator. Running the app on a device work wihtout any problem.
- (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;
}
推荐答案
完全相同的问题一会儿在一个基于PhoneGap的项目我转换为ARC一个回来 - 崩溃在模拟器,但不是在设备上。
I've been fighting this exact same issue for a little while on a PhoneGap-based project I'd converted to ARC a while back - crashing in the simulator but not on the device.
一个新项目和相同的代码工作确定,所以我通过项目配置看看有什么不同。
I created a fresh project and the same code worked OK, so I went through the project configuration to see what was different.
在我的情况下,我有旧的不需要的链接器标记set,具体 -weak_library /usr/lib/libSystem.B.dylib
。
In my case, I had old un-needed linker flags set, specifically -weak_library /usr/lib/libSystem.B.dylib
.
构建设置的链接器标记部分修复了它。
Removing that from the "Other Linker Flags" section of "Build Settings" fixed it.
这篇关于dispatch_once调用导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!