views:

139

answers:

3

In Xcode, say you write an app for the iphone and it has a runtime error in it. What I've been seeing is that it just closes out the program in the simulator but doesn't really hilight or give me any feedback as to what line caused the crash... am I missing something??

Note: I don't consider the console to be very effective since it just spits out an error, but I still need to find where in the heck that bug is stemming from in the code.

A: 

Look in the console (command-shift-R).

I did that but it's very cryptic. Stuff like: Stack: (123123, 234234, 123123, 123123)I wish it'd give me like a page and line number instead or something.. or just hilight the line like in visual studio.
Shnitzel
You can run your program through gdb which is pretty interactive. The output in the console shouldn't be *too* cryptic. The stack is basically the chain of methods that were called leading up to the line that caused the crash. You only really need to look at the first 3-4 lines of the stack since everything is Apple's code.
Maybe i'm missing something here. how do i get to the stack? In the console I had stack followed by a few weird numbers, but I'm sure you're not referring to that one.
Shnitzel
+2  A: 

In the console, above the stack trace, it should say something like "[ClassName selectorName] unrecognized selector sent to instance".

Make sure you really meant to send that selector to that class. If you post what it is, we might be able to help more.

To access GDB, enable breakpoints, add one to your code by clicking in the line number gutter, press build and debug and finally open the debugger (CMD+Shift+Y).

David Kanarek
omigod, i feel so embarrased... I didn't even see that. thanks :)
Shnitzel
Yeah, it doesn't help that they dump a few dozen lines of uselessness after the helpful bit. Did this solve your problem?
David Kanarek
+1 for thorough answer.
Mr-sk
A: 

You can set a global breakpoint on exceptions, which will let you trace the exact point at which they occurred. To do so, select the Run | Show | Breakpoints menu item in Xcode to bring up the breakpoints dialog. Select Global Breakpoints (so that this will be enabled for all of your projects) and create a breakpoint on objc_exception_throw in libobjc.A.dylib.

Now if you start your application by choosing Run | Debug - Breakpoints On, or manually enable breakpoints in the debugger window (Run | Debugger) before running, the application should halt at the point where the exception is thrown. You can then look at the stack trace in the debugger window, where it will highlight the particular line that caused the exception.

Brad Larson