460

How do I set up NSZombieEnabled and CFZombieLevel for my executable in Xcode 4?

Besi
  • 22,579
  • 24
  • 131
  • 223
Chetan
  • 46,743
  • 31
  • 106
  • 145
  • 1
    are you asking about Xcode 4? – Jehiah Jan 08 '11 at 20:15
  • 8
    Why options like this isn't enabled by default is beyond me... – Daniel Magnusson Jul 28 '11 at 10:53
  • 26
    Because it basically prevents any real freeing of memory, which doesn't seem like a good idea to enable by default. – Chetan Jul 29 '11 at 05:24
  • 14
    @Daniel Magnusson: it's a survival-of-the-fittest thing. By making development with XCode unfriendly but workable, you get better devs. Just kidding. – Dan Rosenstark Aug 30 '11 at 16:15
  • 3
    @DanielMagnusson I think this is not a bad idea, AFAIK the "Zombie" mode will change every object to a special kind ob object so that it can detect, when it gets sent messages. So having "Zombie mode" enabled will cause that no object will ever be deallocated, so you should really only used it when tackling such errors. – Besi Jan 21 '12 at 12:01

7 Answers7

753

In Xcode 4.x press

R

(or click Menubar > Product > Scheme > Edit Scheme)

select the "Diagnostics" tab and click "Enable Zombie Objects":

Click "Enable Zombie Objects"

This turns released objects into NSZombie instances that print console warnings when used again. This is a debugging aid that increases memory use (no object is really released) but improves error reporting.

A typical case is when you over-release an object and you don't know which one:

  • With zombies: -[UITableView release]: message sent to deallocated instance
  • Without zombies: EXC_BAD_ACCESS

This Xcode setting is ignored when you archive the application for App Store submission. You don't need to touch anything before releasing your application.

Pressing R is the same as selecting Product > Run while keeping the Alt key pressed.
Clicking the "Enable Zombie Objects" checkbox is the same as manually adding "NSZombieEnabled = YES" in the section "Environment Variables" of the tab Arguments.

GabrieleMartini
  • 1,665
  • 2
  • 19
  • 26
Jano
  • 62,815
  • 21
  • 164
  • 192
  • 27
    Thanks - I was wondering where they hid these options.I now realize this is the same window as in the 'Edit Scheme' menu. – emp Feb 10 '11 at 21:35
  • 1
    Just to add to this, doing it in the "Run" screen sets it for "Test" as well if the "Use the Run action's options" box is checked. If you want to do it for just Test, that box needs to be unchecked – Cameron Aug 15 '11 at 19:01
  • 1
    Please also note that this option appears on Xcode 4.1 (right?) – phi Oct 14 '11 at 08:26
  • 2
    In XCode 5 (and later versions of 4 -- 4.3.x for sure, maybe earlier), this is `CMD-<` (`CMD-SHIFT-comma`), or `Product -> Scheme -> Edit SCheme...` – Olie Oct 02 '13 at 20:32
  • i do prefer this to running profile and then using zombie instruments - seeing the actual message sent was the key to solving the problem, for me – Joel Balmer Feb 07 '14 at 16:39
  • 1
    I am stuck in strange EXC_BAD_ACCESS issue, even the Zombies are skipped, it would be nice if you could direct me for this http://stackoverflow.com/questions/29210057/getting-error-libobjc-a-dylibobjc-assign-strongcast-non-gcobjc-object-objc – Anoop Vaidya Mar 23 '15 at 16:01
70

Jano's answer is the easiest way to find it.. another way would be if you click on the scheme drop down bar -> edit scheme -> arguments tab and then add NSZombieEnabled in the Environment Variables column and YES in the value column...

learner2010
  • 4,157
  • 5
  • 43
  • 68
57

I find this alternative more convenient:

  1. Click the "Run Button Dropdown"
  2. From the list choose Profile
  3. The program "Instruments" should open where you can also choose Zombies
  4. Now you can interact with your app and try to cause the error
  5. As soon as the error happens you should get a hint on when your object was released and therefore deallocated.

Zombies

As soon as a zombie is detected you then get a neat "Zombie Stack" that shows you when the object in question was allocated and where it was retained or released:

Event Type    RefCt     Responsible Caller
Malloc            1     -[MyViewController loadData:]
Retain            2     -[MyDataManager initWithBaseURL:]
Release           1     -[MyDataManager initWithBaseURL:]
Release           0     -[MyViewController loadData:]
Zombie           -1     -[MyService prepareURLReuqest]

Advantages compared to using the diagnostic tab of the Xcode Schemes:

  1. If you forget to uncheck the option in the diagnostic tab there no objects will be released from memory.

  2. You get a more detailed stack that shows you in what methods your corrupt object was allocated / released or retained.

0942v8653
  • 170
  • 1
  • 11
Besi
  • 22,579
  • 24
  • 131
  • 223
5

In Xcode 4.2

  • Project Name/Edit Scheme/Diagnostics/
  • Enable Zombie Objects check box
  • You're done
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
pratap shaik
  • 165
  • 1
  • 9
5

On In Xcode 7

<

or select Edit Scheme from Product > Scheme Menu

select Enable Zombie Objects form the Diagnostics tab

xcode 7 zombie flag

As alternative, if you prefer .xcconfig files you can read this article https://therealbnut.wordpress.com/2012/01/01/setting-xcode-4-0-environment-variables-from-a-script/

IgnazioC
  • 4,554
  • 4
  • 33
  • 46
1

Cocoa offers a cool feature which greatly enhances your capabilities to debug such situations. It is an environment variable which is called NSZombieEnabled, watch this video that explains setting up NSZombieEnabled in objective-C

user2554822
  • 191
  • 2
  • 7
-1

In Xcode > 4.3:

You click on the scheme drop down bar -> edit scheme -> arguments tab and then add NSZombieEnabled in the Environment Variables column and YES in the value column.

Good Luck !!!

Besi
  • 22,579
  • 24
  • 131
  • 223
Dipak Narigara
  • 1,796
  • 16
  • 18