tags:

views:

86

answers:

2

I'd like to know if is it possible to set up Xcode to do the reference counting and show warnings if number of "retain" and "release" are not matching?

+11  A: 

Use the Build > Build and Analyze command, or cmd-shift-A. The static analyzer can catch a lot of mistakes in that area.

Noah Witherspoon
+5  A: 

That's what the Instruments tools are for.

For more retains than releases:

Leaks will tell you when you no longer have a reference to an object, but it still has retains.

Object Alloc will show you all the objects still retained in the system at any point in time. Don't forget that many things should not be released, until your program ends... so it's impossible for the system to know when you have too many retains, except for the Leaks case. Object alloc can also show you, for any object, the complete history of retain and release calls.

If you have more releases than retains, you will know pretty much instantly because the application will crash. In that case, you turn on "NSZombieEnabled" by selecting your executable, "Get Info", and then in the General tab enter the environment variable "NSZombieEnabled" to "YES". Then when you try to access an object you have released you'll see an error message in the log. It can also help a lot to run Object Alloc, and tell it to pay attention to Zombies. Don't leave on the zombie detection though, as it works by never ever releasing memory.

Kendall Helmstetter Gelner