views:

650

answers:

4

I have a simple question about debugging on Xcode and GDB.

I often run into an error:

unrecognized selector sent to instance 0x1081ad0

which makes the program load into GDB. Is there an easy way to examine what instance is located in that memory from GDB?

+5  A: 

po 0x1081ad0

po = Print Object. You can even call methods, like

po [myArray objectAtIndex:0]

Note that it only works on objects, so

po 1

will crash your program.

Steven Canfield
"po 1" won't crash your program -- gdb is smarter than that. It will just print an error message.
Adam Rosenfield
+4  A: 

There are a couple of things you can do.

  1. You can insert a break point that will trigger every time you have an exception, so basically create a break point for this (go to breakpoints and create a new one): -[NSException raise]
  2. Alternatively, you can actually see what the object at that mem location is:

    info symbol 0x1081ad0 or

    info line *0x1081ad0

There's more info at the cocoadev wiki entry for exceptionhandling and debugging tips for objective C at cocoawithlove.

EightyEight
+3  A: 

Steven is correct — the gdb command po is a shortcut for print-object, which actually calls -debugDescription (not -description, as you might expect) on the object provided as an argument. In many cases you'll see the same result from both methods, since one calls the other unless overridden. (See the related Note: callout on this Apple technote for details. Note that in their code sample, po $r3 prints the contents of a PowerPC register, but you can use any object pointer/reference, including Intel registers, etc.)

Also, be aware that print-object will only work on valid objects that haven't been deallocated. It won't help at all if you're sending a message to a borked pointer. Given the error you cited, though, it would seem that it's a valid object instance, it just doesn't implement the method you're trying to invoke.

It's also remotely possible that the object has already been destroyed. This answer should help in that case.


Edit:

There are other ways to "examine" objects in the debugger. I asked this SO question about Xcode data formatters, which is one way you can determine how a custom class appears in the Summary column of the debugger. The documentation linked from that question explain how it works. I've found the summary approach to help a lot with seeing the state of an object.

Quinn Taylor
A: 

Your instance is not valid. You have release the object somewhere else, but you did not clear out your pointer... enable Zombie detection.

Kendall Helmstetter Gelner