Guides and Sample Code


Instruments User Guide

Find Zombies

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 NSZombieEnabled to 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.

To look for zombies in your app
  1. Launch Instruments.

  2. In the profiling template selection dialog that appears, click Zombies.

  3. Choose your device and app from the target device and process lists.

  4. Click Choose to create a trace document.

  5. Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.

  6. 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.

  7. Click the focus arrow (image: ../Art/icon_detail_pane_gotoarrow_2x.png) 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.

  8. Select the Zombie event (or any other event you want to investigate) in the detail pane.

  9. Press Command-3 to display a stack trace for the selected event in the extended detail area of the inspector.

  10. Click the Collapse button (image: ../Art/inline_hidesystemcalls_button_2x.png) 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 (image: ../Art/inline_usercode_icon_2x.png).

  11. Double-click a method in the stack trace to display its code in Instruments.

  12. Click the Xcode button (image: ../Art/inline_xcode_2x.png) 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.