Objective-C Programming Summary (Article 3) Memory Management

Source: Internet
Author: User

Three Principles of Memory Management in OC:

1. Who created (new or alloc) and who release.

2. If you get an object from a method, assume that the retaincount of this object is 1 and has been set to autorelease. You don't need it, and you shouldn't release it. If you need to pass this object further, you can perform retain or copy operations.

3. If you retain an object, you must be responsible for release it.

 

In addition. Carefully read the definition of the release method, which is defined as oneway. This keyword means that the method call does not need to return any messages, and the return of the method does not guarantee that the method has been completed. That is to say, when you call [OBJ release], retaincount does not necessarily respond immediately. Therefore, retaincount is unreliable unless you are willing to sleep for a while to wait for the release to complete.

 

Another point is that if the attributes of a class are defined by @ property (retain), it is best to do two things at the same time when releasing, 1, release it, 2, set it to nil through setter. To avoid confusion caused by pointer pointing to random location of memory.

 

The third point to be supplemented is that when a created object is returned using a factory-like method, the object must be returned in the following way:

Return [product autorelease];

 

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.