[Reading Notes] iOS-reference count, Reading Notes ios-count

Source: Internet
Author: User

[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.