With introduction of the Objective-C 2.0, many people were relieved from headache of managing memory. The garbage collector of Obj-C 2.0 is very easy to use and people can forget about if certain object should be retained or released, and so on. Yes. The garbage collector is good. However I still write with retain and release method. Why? Because I can control memory space! I can get rid of memory space which is not used any more very quickly.
For group projects in which there are some people who don’t understand memory management well, the garbage collector is very good choice to adopt. However, one solution can’t be answers for all problems. So, old retain, release, autorelease are still necessary.
Then, why there are people who don’t understand mechanism of object retention? Probably one reason is when objects are released is different from when an object is created with convenience methods and general -alloc and -init… methods.
For example, an object created with convenience method will have its retain count as a value 1. You can use this object until the variable which point to this object is still in the scope. However, if it is out of scope, it will be a candidate for removal. So, if you want to keep this kind of object without worrying about the scope, you should retain it. Then its retain count becomes 2, and after using it completely, you release it. Then its retain counter becomes 1 and it becomes a candidate for removal by so-called semi-garbage collector, or the old pool-based memory management mechanism.
On the other hand, an object created with -alloc and -init method doesn’t become a candidate for removal when its retain count is 1. It is up to you and up to the release method call. So, you can keep your object which is out of scope even when the retain count is 1. The reason is that pool doesn’t manage it. So, it doesn’t autorelease it. Only explicit release call can dispose the object. So, you don’t need to call the retain method if another object or something else also doesn’t want to keep it.
It is very simple!