stoflow
stoflow

Reputation: 163

dispatch_once call causes crash

dispatch_once call causes crash (in simulator) after I've converted my project to ARC.

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.

Based on loggings, and breakpoints my code have not run into the dispatch_once call's block.

I didn't know the reason, so I've just commented out the dispatch_once call. My app haven't crashed without that call.

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;
}

Upvotes: 4

Views: 3657

Answers (1)

aroldan
aroldan

Reputation: 26

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.

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.

Upvotes: 1

Related Questions