[Reading Notes] iOS-reference count, Reading Notes ios-count
Cocoa uses a technology called reference counting, which is sometimes called reserved counting. Each object has an associated integer called its reference counter or retention counter. When a code segment needs to access an object, the Code adds the reserved counter value of this object to 1, indicating "I want to access this object ". When this code ends object access, the reserved counter value of the object is reduced by 1, indicating that it no longer accesses the object. When the reserved counter value is 0, it indicates that no code is available to access the object, so the object will be destroyed, and the memory occupied by it will be recycled by the system for reuse.
When an object is created using the alloc, new method, or copy message (a copy of the receiving object is generated), the reserved counter value of the object is set to 1. to increase the counter value of an object, you can send a retain message to the object. To reduce the counter value of an object, you can send a release message to the object.
When an object is about to be destroyed because its reserved counter is 0, Objective-C automatically sends a dealloc message to the object. You can rewrite the dealloc method in your own object. You can release all allocated resources in this way. Do not directly call the dealloc method. You can use Objective-C to call the dealloc method when you need to destroy an object. To get the current value of the reserved counter, you can send a retainCount message.
References: Objective-C basic tutorial