4

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;
}
stoflow
  • 163
  • 1
  • 10
  • 1
    lets see some code ^^ 100 reasons why this could happen – Daij-Djan Nov 29 '12 at 13:16
  • there is nothing arc would change.. and that exact code doesnt crash for me ( just tried ) – Daij-Djan Nov 29 '12 at 13:59
  • I've added a sample code to the original question. – stoflow Nov 29 '12 at 14:00
  • I see, but something became f***ed up after converting to ARC... I haven't had any problem with the same code before ARC. Just one more thing : converting to ARC meant that I had to change the compiler to Apple LLVM from LLVM GCC... – stoflow Nov 29 '12 at 14:02
  • Note: If I disable ARC, the mentioned sample codes runs fine... – stoflow Nov 29 '12 at 14:49
  • It seems like there is something missing from this question. Are you saying that if you run the code *exactly* as posted in your question that you get an `EXC_BAD_ACCESS`? Also, what version of Xcode are you using? – NJones Nov 29 '12 at 20:54
  • Yes, exactly. Xcode 4.5.2. If I disable ARC, it does not crash. Can it be because I have linked libraries that aren't built using Apple's LLVM, but with LLVM GCC, or GCC (I don't know exactly)? – stoflow Nov 30 '12 at 09:31
  • Having the same problem, did you find any other solution than checking other linker flags? My code still crashes in the simulator :/ – Dominik Hadl May 12 '13 at 11:41

1 Answers1

1

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.

aroldan
  • 26
  • 2