"Reading notes" ios-reference counting

Source: Internet
Author: User

Cocoa employs a technique called reference counting, sometimes called a retention count. Each object has an integer associated with it, called its reference counter or reserved counter. When a piece of code needs to access an object, the code adds 1 of the object's retention counter value, which means "I want to access the object." When this code ends the object access, the object's retention counter value is reduced by 1, indicating that it no longer accesses the object. When the hold counter value is 0 o'clock, it means no longer has code access to the object, so the object is destroyed and its occupied memory is reclaimed by the system for reuse.

When you use the Alloc,new method or create an object by using a copy message that generates a replica of the receiving object, the object's retention counter value is set to 1. To increase the retention counter value of an object, you can send an retain message to the object. To reduce the retention counter value of an object, you can send an release message to the object.

Objective-c automatically sends an DEALLOC message to an object when an object is about to be destroyed because its retention counter is 0. You can override the Dealloc method in your own object. You can release all related resources that have been allocated in this way. Be sure not to call the Dealloc method directly. You can use Objective-c to call the Dealloc method when you need to destroy an object. To get the current value of the hold counter, you can send a retaincount message.

Reference: "Objective-c Basic Course"

"Reading notes" ios-reference counting

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.