Objective-c: A detailed explanation of the idea of preserving the counter (retention of objects and release of ownership)

Source: Internet
Author: User

Retention of objects and release of ownership:

int main (int agrs,char *argv[]) {@autoreleasepool {Person *person = [[Person alloc]initwithname:@ ' Tom ']; Retaincount=1[Person Setname:name]; Retaincount = 2;[person release];}} @interface Person@property (Nonatomic,retain) (nsstring*) name; @endin detail: A Perosn object is created in main function main, at which point the main function has ownership of the object person, retains the counter retaincount=1, and immediately thereafter, because the instance variable of class person name type retain is a strong reference, , when the person object in the main function calls the SetName method, the class person also has ownership of the person object,Therefore, the person class will keep the counter plus 1 to hold the person object, at this time of the retaincount=2; According to the principle that the object who created, who released, the main function in the main function of the Perosn object is used by the main function to release its ownership, The main function is immediately removed. At this point, keep the counter retaincount=1, at which time the system does not call the Dealloc method to destroy the object, and the object is reserved to the class person. By the end, after the class person has run out of object person, the class person should also release ownership of the person object, so call the overridden Dealloc method to release the person ownership in this method, retaincount= 0, after completing the "Super Dealloc" of the parent class, the Perosn object is completely destroyed. In short two words: (1) Who calls this object, it must obtain its ownership, the object is retained, to prevent direct destruction;(2) who created the object, who was responsible for releasing it; who called the object, who was responsible for releasing it.

Objective-c: A detailed explanation of the idea of preserving the counter (retention of objects and release of ownership)

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.