Summary of manual memory management rules

Source: Internet
Author: User

1. If you need to keep an object from being destroyed, you can use retain. After you have finished using the object, you need to use release to destroy

2. Sending a release message to an object does not destroy the object, and the object is destroyed only if the object's reference count is reduced to 0 o'clock. The system then sends a DEALLOC message to the object to release its memory.

Releasing any object that uses the retain or Copy,mutablecopy,alloc or the new method, as well as properties with retain and copy attributes, requires overriding the Dealloc method so that the object can release the instance variables when it is disposed.

3. Objects that are automatically freed when the auto-release pool is emptied. The system sends a release message to each object in the pool each time the automatic release pool is destroyed. If the object reference count in the pool is reduced to 0, the system sends a DEALLOC message to destroy the object.

4. If the object is no longer needed in the method but needs to be returned, you can send an autorelease message to this object to mark the object for deferred release. The autorelease message does not affect the reference count of this object.

5. When the app terminates, the in-memory objects are freed, regardless of whether they are automatically freed from the pool.

When developing a cocoa or iOS application, the automatic release pool will be created and emptied as the application runs (each event will occur). In this case, if you want the automatic free pool to be emptied after an object that automatically frees the pool can also exist, the object needs to use the Retain method, as long as the reference count of these objects is greater than the number of autorelease messages sent to survive the pool being cleaned up.

                                 and nbsp                                  &NBS P                                  &NBS P                                   ;                      --excerpt from Objective-c programming version sixth

                                                           ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                  --[Mei]stephen G.kochan

--Lin Fan Jun Zhu Yixin

Summary of manual memory management rules

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.