How do I set up NSZombieEnabled in Xcode 4? - objective-c

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

In Xcode 4.x press
⌥⌘R
(or click Menubar > Product > Scheme > Edit Scheme)
select the "Diagnostics" tab and 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:
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.

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

I find this alternative more convenient:
Click the "Run Button Dropdown"
From the list choose Profile
The program "Instruments" should open where you can also choose Zombies
Now you can interact with your app and try to cause the error
As soon as the error happens you should get a hint on when your object was released and therefore deallocated.
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:
If you forget to uncheck the option in the diagnostic tab there no objects will be released from memory.
You get a more detailed stack that shows you in what methods your corrupt object was allocated / released or retained.

In Xcode 4.2
Project Name/Edit Scheme/Diagnostics/
Enable Zombie Objects check box
You're done

On In Xcode 7
⌘<
or select Edit Scheme from Product > Scheme Menu
select Enable Zombie Objects form the Diagnostics tab
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/

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

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 !!!

Related

IOS 5 Arc Is it still a good practice to release your object as soon as you are done.

I am using the NSXMLParser and would like to release some of the objects as soon as I am done with them in the parsing. However, I am not sure if that would mess up the automatic reference counting in IOS5 ? Is that a bad practice to release the objects asap in the code in IOS5 when you are done with them?
Few things, one of them should be your answer:
in an ARC project you can't call retain or release. A call to those methods will produce a compile time error, so you really cannot do any manual memory management any more.
on the other hand you can turn ARC on and off on a per file basis. That's very easy to do- open the project settings, click on "build phases", open the "compile sources" strip, then select the desired .m file and hit "enter" on the keyboard - in the popup window enter "-fno-objc-arc". Close and re-start Xcode (they still have a bug with this, so ... ). Now ARC is disabled for this file - you can use retain and release in the code and everything
however there's also another way to go - use a release pool. In general if you are about to consume lot of memory and you wanted it released faster you should use a separate memory pool, it's very easy to do that and the new iOS5 way looks like this:
#autoreleasepool {
// code code code code
}
So, all the allocations happening between the curly brackets will be released when you get out of the block - there you go :)

Where to set automatic reference counting - xcode 4.2

When I created my project, I checked the "Use Automatic Reference Counting" to on. Now, I'm thinking of turning it off but I can't find where to change it.
One more thing, is it advisable to use ARC for iPad/iPhone apps?
Open the project file.
Under Build Settings tab search for "Objective-C Automatic Reference Counting".
Set the value for it.
Yes, sure if you want to deliver a product for iOS 4.0 and above you can definitely use ARC.
You can turn off "Use Automatic Reference Counting" by clicking on your project in the navigator (the top item in the finder-like thing on the left) click on your target, select "Build Settings" and turn off the option "Objective-C Automatic Reference Counting"
If programmer is novice than it is good advice to let them do it manually , otherwise his memory fundamentals will be never clear , Otherwise it is just fine to use it.
Go to the Project Menu->Target->Build Settings->Objective-C Automatic Reference Counting, then you can set here Yes or No. if You are a beginner and You can't handle memory issue yourself then you can enable ARC.

vb.net Add Watch stop when value changes

I know in the older versions of Visual Studio, there was an "Add Watch" option where you can choose to stop execution when the value of the field changed. I am using VS 2010, and I can't figure out how to hit the breakpoint when the value of the field changes.
Any ideas?
Data breakpoints is what I remember, your description matches. It used a processor feature, it requires the address of the variable and the size, the processor automatically generates a trap when it detects a write to the memory address. Very nice debugging tool.
Sadly no longer available in managed code, the garbage collector messes it up because it moves objects around while compacting the heap. Which changes their address. The interface between the garbage collector and the debugger isn't strong enough to allow the debugger to track these moves while the compacting is taking place at runtime. No doubt to avoid a serious amount of overhead.
The next best thing you got is a property setter. You can set a breakpoint on it.
Right click on the breakpoint and hit Condition. You should be able to do the same from here.
In vb.net 2010 (I am using the express edition) - set a breakpoint and run up to it. Right click the variable/control name you wish to watch then select add watch from the context menu.
The watch window will appear.
You can type variable names directly in to the watch window, providing they are in scope.
You can right click on a break point and then choose Condition. In the condition box type the name of the variable and select the 'Has Changed' radio button.

How to use NSzombie in xcode? [duplicate]

I have an app that is crashing with no error tracing. I can see part of what is going on if I debug, but can't figure out which object is "zombie-ing".
Does anybody know how to enable NSZombie in Xcode 4?
Environment variables are now part of the "scheme".
To edit the scheme and turn on zombies:
In the "Product" menu, select "Scheme" > "Edit Scheme...".
Go to the "Run Foo.app" stage in the left panel, and the "Arguments" tab on the right.
Add NSZombieEnabled to the "Environment Variables" section and set the value to YES, as you could in Xcode 3.
In Xcode 4.1 and above, there's also a checkbox on the "Diagnostics" tab of the "Run" stage to "Enable Zombie Objects".
With Xcode 6.4:
I encountered the same problem with troubleshooting EXC_BAD_ACCESS and had hard time to find the setting with Xcode 4.2 (the latest one that comes with iOS5 SDK). Apple keeps on moving things and the settings are no longer where they used to be.
Fortunately, I've found it and it works for the device, not just Simulator. You need to open the Product menu in the Xcode, select Edit scheme and then choose the Diagnostics tab. There you have "Enable Zombie Objects". Once selected and run in debugger will point you to the double released object! Enjoy!
In short
Product->Edit Scheme->Diagnostics-> Click Enable Zombie Objects
Product > Profile will launch Instruments and then you there should be a "Trace Template" named "Zombies". However this trace template is only available if the current build destination is the simulator - it will not be available if you have the destination set to your iOS device.
Also another thing to note is that there is no actual Zombies instrument in the instrument library. The zombies trace template actually consists of the Allocations instrument with the "Enable NSZombie detection" launch configuration set.
It's a simple matter of setting an environment variable on your executable (NSZombieEnabled = YES), and then running/debugging your app as normal.If you message a zombie, your app will crash/break to debugger and NSLog a message for you.
For more information, check out this CocoaDev page: http://www.cocoadev.com/index.pl?NSZombieEnabled
Also, this process will become much easier with the release of 10.6 and the next versions of Xcode and Instruments. Just saying'. =)
Product > Profile will pop up Instruments. Select zombies from the panel and go nuts.
Go to Product - Scheme - edit scheme - Arguments - Environment Variables set NSZombieEnabled = YES
In xcode 4.2
Goto, Product -> edit scheme -> click Run yourappname.app -> Diagonostics -> Enable Zombie object.
Here's a video and explaination how to use Instruments and NSZombie to find and fix memory crashes on iOS:
http://www.markj.net/iphone-memory-debug-nszombie/
As of Xcode 3.2.5 and Snow Leopard (Mac OS X 10.6), you can run your code through the Zombies instrument: Run > Run with Performance Tool > Zombies. That allows you to see particular objects and their retain counts on a timeline.
In Xcode 4.5.2 goto Product -> Edit Scheme -> and Under the Diagnostics tab check the check box in between Objective C and Enable Zombie Objects and Click on OK
To enable Zombie logging double-click the executable in the executables group of your Xcode project. At this point click the Arguments tab and in the Variables to be set in the environment: section, make a variable called NSZombieEnabled and set its value to YES.
In XCode 4.0: To detect NSZombie in Instruments, select the Simulator as your target (can't detect NSZomboe on device). Run Instruments (CMD+I) and select "Zombies" trace template. Enjoy.
In the preferences of your executable add the environment variable NSZombieEnabled and set the value to YES.
in ur XCODE (4.3) next the play button :) (run)
select : edit scheme
the scheme management window will open
click on the Arguments tab
you should see : 1- Arguments passed on launch
2- environment variables
inside the the (2- environment variables) place
Name: NSZombieEnabled
Value: YES
And its done....
NSZombieEnabled is used for Debugging BAD_ACCESS,
enable the NSZombiesEnabled environment variable from Xcode’s schemes sheet.
Click on Product⇒Edit Scheme to open the sheet and set the Enable Zombie Objects check box
this video will help you to see what i'm trying to say.

Watching variables in Xcode

I'm trying to watch a variable with Xcode. I'm following the instructions in here by pausing at a breakpoint, selecting Run > Variables View > .... but with the exception of "Enable Data Formatters" the rest of the options are all greyed out. Any ideas?
I'm using Xcode version 3.1.3.
I haven't gotten watchpoints created from the Run menu to work for me either, unfortunately. One thing to be aware of is that when a variable goes out of scope, the watchpoint may become invalid.
If you don't mind getting a little more in-depth, you can use some low-level gdb commands to set a watchpoint for the address of the memory itself. For example, in the guide you linked to, they show how to watch the variable path which is a pointer with the value 0xbfffeb70. To manually set a watchpoint for that address, click in the debugger console (where the debugging output is printed) after the "(gdb)" prompt and type something like this:
watch *((int*)0xbfffeb70)
The cryptic syntax is necessary because gdb expects inputs as C expressions. For a little more detail, visit this link and jump to the section titled "Using hardware watchpoints". (I'm testing on an Intel machine, not sure how PowerPC handles it.) When you set watchpoints this way, Xcode will alert you with a drop-down sheet when a watchpoint is reached and tell you how the value was changed, and gdb will print the same info in the console.
I just ran into this problem. Here is a solution: right click on the variable name and select "View variable in window" from the menu which appears. It should be near the bottom.
Add a breakpoint. Right click in the watch list of the debug area and choose "Add expression..."
If you are getting a different menu, you have to click off of the currently highlighted variable so that nothing is highlighted when you right click.
The Answers given here only work if you use the gdb compiler. For those of you who are looking for an option to set a watchpoint with the lldb compiler I have bad news:
It's not working jet (XCode 4.3.2 with lldb 3.1) even though the lldb docs say you can.
Check out this Email. The lldb commands compared to the gdbs can be found here
I was trying to figure this out in XCode 5. I finally found a "Variables view" button at the bottom right of the output console. It's the little rectangle that will be gray on the left, white on the right if it's not enabled. I'm not sure if this is in XCode 3, but I expect most people have upgraded anyway.