简体   繁体   English

如何查看Xcode调试器中的对象?

[英]How do I look at an object in Xcode's debugger?

I have a simple question about debugging on Xcode and GDB. 我有一个关于在Xcode和GDB上调试的简单问题。

I often run into an error: 我经常遇到一个错误:

unrecognized selector sent to instance 0x1081ad0

which makes the program load into GDB. 这使得程序加载到GDB中。 Is there an easy way to examine what instance is located in that memory from GDB? 有没有一种简单的方法可以从GDB中检查该内存中的实例?

po 0x1081ad0

po = Print Object. po =打印对象。 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 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. Steven是正确的 - gdb命令poprint-object的快捷方式,它实际上在作为参数提供的对象上调用-debugDescription (不是-description ,如您所料)。 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.) (有关详细信息,请参阅相关说明: 此Apple技术 说明中的标注。请注意,在其代码示例中, po $r3打印PowerPC寄存器的内容,但您可以使用任何对象指针/引用,包括Intel寄存器等)

Also, be aware that print-object will only work on valid objects that haven't been deallocated. 另外,请注意print-object仅适用于尚未释放的有效对象。 It won't help at all if you're sending a message to a borked pointer. 如果您向borked指针发送消息,它将毫无帮助。 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. 我问过这个关于Xcode数据格式化器的问题,这是一种可以确定自定义类如何出现在调试器的Summary列中的方法。 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. 我发现总结方法可以帮助我看到对象的状态。

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] 你可以插入一个断点,每次有异常时都会触发,所以基本上为此创建一个断点(转到断点并创建一个断点): - [NSException raise]
  2. Alternatively, you can actually see what the object at that mem location is: 或者,您实际上可以看到该mem位置的对象是什么:

    info symbol 0x1081ad0 or 信息符号0x1081ad0或

    info line *0x1081ad0 信息行* 0x1081ad0

There's more info at the cocoadev wiki entry for exceptionhandling and debugging tips for objective C at cocoawithlove . 有关cocoawithlove 目标C的 异常处理调试技巧,请 参阅cocoadev wiki条目

Your instance is not valid. 您的实例无效。 You have release the object somewhere else, but you did not clear out your pointer... enable Zombie detection. 你已经在其他地方释放了这个对象,但你没有清除你的指针...启用Zombie检测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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