简体   繁体   中英

How to track down the line that's causing a runtime error?

My app is crashing with this error message:

'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: 0)'

and I am trying to find where this is occuring in my code. I have set NSZombiesEnabled in the environment variables, however it's pretty hard to figure out the location. Any idea on how to debug?

I typed in bt and here's what I see:

 frame #0: 0x30d1832c libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x3597d20e libsystem_c.dylib`pthread_kill + 54
    frame #2: 0x3597629e libsystem_c.dylib`abort + 94
    frame #3: 0x35055f6a libc++abi.dylib`abort_message + 46
    frame #4: 0x3505334c libc++abi.dylib`_ZL17default_terminatev + 24
    frame #5: 0x36c38356 libobjc.A.dylib`_objc_terminate + 146
    frame #6: 0x350533c4 libc++abi.dylib`_ZL19safe_handler_callerPFvvE + 76
    frame #7: 0x35053450 libc++abi.dylib`std::terminate() + 20
    frame #8: 0x35054824 libc++abi.dylib`__cxa_rethrow + 88
    frame #9: 0x36c382a8 libobjc.A.dylib`objc_exception_rethrow + 12
    frame #10: 0x3428d50c CoreFoundation`CFRunLoopRunSpecific + 404
    frame #11: 0x3428d36c CoreFoundation`CFRunLoopRunInMode + 104
    frame #12: 0x3826f438 GraphicsServices`GSEventRunModal + 136
    frame #13: 0x36d27cd4 UIKit`UIApplicationMain + 1080
    frame #14: 0x00051cdc AppName`main + 80 at main.m:16

Add exceptions as a breakpoint. Assuming Xcode 4+...

  1. At the top of the navigator frame (where you view files and such), you will see a little icon that looks like a sideways arrow.
  2. That will show the breakpoint navigator. At the bottom of that view, click the + to add a breakpoint.
  3. Click on "Add exception breakpoint..."

The default option is probably fine... you want to break on throw of all exceptions.

Now, when you run, you will get a breakpoint anywhere your app throws an exception.

Is the error happening in your testing? Are you seeing it from crash logs from iTunes Connect?

If you are able to reproduce it locally, run the scenario to duplicate the error and when the debugger becomes active, type "bt" at the (gdb) prompt in the Console for a stacktrace with a line number.

Try to add this part of code in your AppDelegate :

void uncaughtExceptionHandler(NSException *exception);

void uncaughtExceptionHandler(NSException *exception) {
    NSLog(@"CRASH: %@", exception);
    NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

// your previous code here
    return YES;
}

Next time your error occurs, you should have some additional infos on the crash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM