The Zombies profiling template uses the Allocations instrument to measure general memory usage in your app, with a focus on the detection of overreleased “zombie” objects—that is, objects that are called after they’ve been released and no longer exist.
In this template, the Allocations instrument is configured to enable NSZombie detection. This is a debug mode that sets the environment variable
true, directing the compiler to substitute an object of type
NSZombie for any objects that are released to a reference count of zero—no more of those objects should exist. If a message is then sent to one of these deallocated objects (which are now
NSZombie objects), the zombie is flagged, the app crashes, recording stops, and a Zombie Messaged dialog appears. You can then examine the retain and release history of the zombie object to determine exactly where the problem occurred.
In the profiling template selection dialog that appears, click Zombies.
Choose your device and app from the target device and process lists.
Click Choose to create a trace document.
Click the Record button () in the toolbar (or press Command-R) to begin recording.
Use your app normally.
If a call is made to a deallocated object, a flag is inserted in the timeline pane and a Zombie Messaged dialog appears, indicating that a message was sent to a deallocated object at a specific memory address.
If you close the Zombie Messaged dialog, you can reopen it by clicking the flag.
Click the focus arrow () next to the zombie’s memory address to display the memory history of the zombie object in the detail pane, along with corresponding reference counts and method calls.
Select the Zombie event (or any other event you want to investigate) in the detail pane.
Press Command-3 to display a stack trace for the selected event in the extended detail area of the inspector.
Click the Collapse button () in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.
Calls made by your app are colored black and preceded by a user code icon ().
Double-click a method in the stack trace to display its code in Instruments.
Click the Xcode button () at the top of the detail pane to open the code in Xcode for editing.
Although Instruments can help you detect zombies, you still need to look carefully through the related memory history and your code in order to identify and resolve the problem. The following scenarios are common causes of zombies:
An object has already been released (or autoreleased), and your app tries to release it again.
An object hasn’t been retained when it should have been.
Some other call is made to an object after it has been released.