Cocoa memory Management: Retain,release and Autorelease.
Each object maintains a retention counter. When the object is created, its hold counter value is 1, the object is retained, the counter value is reserved, and the object is freed, the counter value is reduced by 1, and when the hold counter value is 0 o'clock, the object is destroyed. When an object is destroyed, the object's Dealloc method is called first, and then its occupied memory is reclaimed for use by other objects.
When an object receives a autorelease message, its reserved counter value does not change immediately. Instead, the object is simply put into the nsautoreleasepool. When the auto-free pool is destroyed, the release message is sent to all objects in the body pool. All objects that are automatically freed have their reserved counter values minus 1. If the reserved counter value is 0, the object is destroyed. When using AppKit, OBJECTIVE-C will create and destroy the auto-free pool for you at the appropriate time, for example, when the current user event has been processed. In addition, you are responsible for creating your own auto-release pool, and the template for the Foundation Library tool contains the code.
The 3 rules in cocoa about objects and their reserved counters.
1, if an object is obtained using the New,alloc or copy operation, the object's retention counter value is 1.
2, if an object is obtained by any other method, it is assumed that the object has a retention counter value of 1 and has been set to be freed automatically.
3, if an object is preserved, you must keep the Retain method and the release method in equal number of uses.
Reference: "Object-c Basic Course"
"Reading notes" ios-memory management