IOS memory reference count note

Source: Internet
Author: User

In iOS, objective-C uses reference counting to manage memory. Recently, when writing animation effects, it encountered a memory leakage problem.

For the delegate declaration of objects, we recommend that you use assign in the objective-C Programming Guide. If you use retain to add the declaration method of reference count, improper management can easily lead to loop reference problems, resulting in the memory cannot be released. However, this write animation finds an exception, that is, when the value of the cabasicanimation delegate value is assigned, the reference count of the object is increased by one. When the removeoncompletion of cabasicanimation is no, the animation will not be removed after the animation ends. A circular reference is formed and the object cannot be released. Do not set the removeoncompletion of an animation to no after the delegate of the animation is specified. When you are not sure whether the animation is completed, it is best to call the removeanimationforkey method of calayer to manually remove the animation. Refer to the delegate of cabasicanimation.

About the delayed execution method-(void) performselector :( SEL) aselector withobject :( ID) anargument afterdelay :( nstimerinterval) delay; During the call, the reference count of the nsobject that calls it is also added to 1, before releasing the memory, call the corresponding + (void) cancelpreviousperformrequestswithtarget :( ID) atarget; Method to clear requests in the queue.

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.