Cocoa关于对象及保留计数器的3条规则:
1、如果使用new、alloc或copy操作获得一个对象,则该对象的保留计数器值为1。
2、如果通过其他任何方法获得一个对象,则假设该对象的保留计数器值为1,而且已经被设置为自动释放。
3、如果保留了某个对象,则必须保持retain方法和release方法的使用次数相等。
其实归结起来就一句话:如果使用了new、alloc或copy获得一个对象,则必须释放或自动释放该对象。
Cocoa has three rules about objects and their retain counts:
■ If you get the object from a new, alloc, or copy operation, the object has a retain count of 1.
■ If you get the object any other way, assume it has a retain count of 1 and that it has been autoreleased.
■ If you retain an object, you must balance every retain with a release.
You’ll be safe if you remember the mantra, “If I get it from new, alloc, or copy, I have to release or autorelease it.”