Objective C - getting line number or full stack trace due to debugger error?

Is it possible to get the line number for the source code (or something that helps debug where the problem is) from the debugger that shows where the problem is coming from?

I get an error message:

-[NSCFArray objectAtIndex:]: index (-1 (or possibly larger)) beyond bounds (9) 

which obviously means that I'm going out of bounds at some point, however, if possible, I would like more information to help me solve the problem.

I put a breakpoint and try to go through the program line by line, but this is a tedious process. Thank!

+21
objective-c xcode cocoa
May 08 '12 at 15:09
source share
2 answers

When the debugger stops, go to "Debug Navigator" and make sure the slider at the bottom is on the right.

Scan your eye down from the point where the exception is thrown, and you should end up with your own code. Click on the corresponding method / function name and the code will open in the editor.

enter image description here

enter image description here

If you do not see any of your own methods in the stack trace, the exception can be performSelector through a call to performSelector -style, in which case the stack trace will disappear. If so, you can get better information by adding an β€œOn Throw” exception exception point. First, switch to the breakpoint navigator:

enter image description here

Then click on the plus and select "Add Exception Control Point ..."

enter image description here

Create a breakpoint "On Throw":

enter image description here

This will stop the debugger in the exact place when the exception is thrown and you get the best stack trace. It is a good idea that an exception-throwing time like this is constantly turned on, although sometimes you get internal exceptions from Apple code (for example, when using QLPreviewController, MPMoviePlayerController).

+79
May 08 '12 at 3:19 p.m.
source share

You should also consider using NSSetUncaughtExceptionHandler. (You can save the crash log to disk, check the next run if a new crash log has been saved, bind it to email, etc.)

put this in your didFinishLaunchingWithOptions method:

 NSSetUncaughtExceptionHandler(&exceptionHandler); 

and implement an exception handler:

 void exceptionHandler(NSException *exception) { NSLog(@"%@",[exception name]); NSLog(@"%@",[exception reason]); NSLog(@"%@",[exception userInfo]); NSLog(@"%@",[exception callStackSymbols]); NSLog(@"%@",[exception callStackReturnAddresses]); } 
+7
Aug 28 '14 at 14:20
source share



All Articles