views:

203

answers:

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.

A: 

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.

Lucas McCoy
A: 

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.

nall
+5  A: 

I would recommend the following:

  • Use NSZombieEnabled to monitor when messages are sent to deallocated NSObjects
  • Use Instruments to track object allocations and/or memory leaks
fbrereto
Do not do these things together. NSZombieEnabled will cause "leaks" to show up in Instruments.
nall
+1: NSZombie objects are the Apple-prescribed way to deal with something like this. And like nall said, don't use Instruments with zombies enabled.
Marc W
A: 

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.

Matt Gallagher
A: 

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.

Peter Hosey
The crash has been a bit difficult to reproduce, but I do know from the crash logs that the fault is occuring in `objc_msgSend`.
mipadi
Then the next step is to run it with NSZombieEnabled.
Peter Hosey