简体   繁体   English

如何在Cocoa应用程序中追踪分段错误?

[英]How can I track down a segmentation fault in a Cocoa application?

I'm having a problem with a Cocoa application I am writing. 我正在写一个Cocoa应用程序的问题。 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. 它必须解析每小时更新的带时间戳的文件,并且在测试期间,由于分段错误,它会在晚上11:45左右持续崩溃。 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? Xcode安装提供了哪些工具来跟踪对象分配,并且(希望)告诉我,我是否正在发送已解除分配的对象?

I am using Mac OS X 10.5. 我使用的是Mac OS X 10.5。

I would recommend the following: 我会推荐以下内容:

  • Use NSZombieEnabled to monitor when messages are sent to deallocated NSObjects 使用NSZombieEnabled监视何时将消息发送到解除分配的NSObject
  • Use Instruments to track object allocations and/or memory leaks 使用Instruments来跟踪对象分配和/或内存泄漏

The way I do it is by using a command line tool called gdb . 我这样做的方法是使用一个名为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. 注意: gbd可用于C,C ++和Objective-C程序。

Have you run the program under gdb? 你在gdb下运行程序了吗? This should allow you to inspect the stack and variables when it SIGSEGVs. 这应该允许您在SIGSEGVs时检查堆栈和变量。

To track allocations, use malloc_history . 要跟踪分配,请使用malloc_history This requires the MallocStackLogging environment variable to be set. 这需要设置MallocStackLogging环境变量。

A quick point: using a deallocated memory location usually results in a EXC_BAD_ACCESS exception. 快速点:使用解除分配的内存位置通常会导致EXC_BAD_ACCESS异常。 If that's the crash reason you're seeing then you're correct in assuming it's a deallocation problem. 如果那是你看到的崩溃原因那么你认为这是一个解除分配问题是正确的。

Run it in Xcode's debugger (which is gdb with a GUI on top) and reproduce the crash. 在Xcode的调试器(在顶部带有GUI的gdb)中运行它并重现崩溃。 Then, look at the stack trace. 然后,查看堆栈跟踪。

Messaging a deallocated object usually has the top frame in objc_msgSend. 传递解除分配的对象通常具有objc_msgSend中的顶部框架。 The next step then is to run the app with NSZombieEnabled and reproduce the crash; 接下来的步骤是使用NSZombieEnabled运行应用程序并重现崩溃; the zombie will identify itself. 僵尸会识别自己。

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

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