如何追踪 Cocoa 应用程序中的分段错误?
我正在编写的 Cocoa 应用程序遇到问题。它必须解析每小时更新一次的带时间戳的文件,在测试过程中,由于分段错误,它在晚上 11:45 左右持续崩溃。我假设我必须向一个已被释放的对象发送消息。 Xcode 安装提供了哪些工具来跟踪对象分配并(希望)告诉我是否正在向已释放的对象发送消息?
我使用的是 Mac OS X 10.5。
I'm having a problem with a Cocoa application I am writing. It has to parse a timestamped file that is updated every hour, and during testing it keeps crashing consistently at around 11:45 PM due to a segmentation fault. I'm assuming I must be messaging an object that has been deallocated. What tools are provided with the Xcode install to track object allocations and (hopefully) tell me if I am messaging an object that has been deallocated?
I am using Mac OS X 10.5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议如下:
NSZombieEnabled
来监视消息何时被发送到释放的 NSObjectsI would recommend the following:
NSZombieEnabled
to monitor when messages are sent to deallocated NSObjects我的方法是使用名为
gdb
的命令行工具。 这里是有关如何使用它的教程。您必须学习它的一些命令,但一旦您学会了,使用它几乎是一种乐趣。注意:
gbd
可用于 C、C++ 和 Objective-C 程序。The way I do it is by using a command line tool called
gdb
. Here is a tutorial on how to use it. You'll have to learn a few of it's commands, but once you do it's almost a pleasure to use.Note:
gbd
can be used on C, C++, and Objective-C programs.你在gdb下运行过程序吗?这应该允许您在 SIGSEGV 时检查堆栈和变量。
要跟踪分配情况,请使用 malloc_history。这需要设置
MallocStackLogging
环境变量。Have you run the program under gdb? This should allow you to inspect the stack and variables when it SIGSEGVs.
To track allocations, use malloc_history. This requires the
MallocStackLogging
environment variable to be set.快速说明一下:使用已释放的内存位置通常会导致
EXC_BAD_ACCESS
异常。如果这是您看到的崩溃原因,那么您认为这是释放问题是正确的。A quick point: using a deallocated memory location usually results in a
EXC_BAD_ACCESS
exception. If that's the crash reason you're seeing then you're correct in assuming it's a deallocation problem.在 Xcode 的调试器(顶部有 GUI 的 gdb)中运行它并重现崩溃。然后,查看堆栈跟踪。
向已释放的对象发送消息通常在 objc_msgSend 中具有顶部框架。下一步是使用 NSZombieEnabled 运行应用程序并重现崩溃;僵尸会识别自己的身份。
Run it in Xcode's debugger (which is gdb with a GUI on top) and reproduce the crash. Then, look at the stack trace.
Messaging a deallocated object usually has the top frame in objc_msgSend. The next step then is to run the app with NSZombieEnabled and reproduce the crash; the zombie will identify itself.