Objective-C memory management principles

Source: Internet
Author: User

Objective-C memory management principles
The basic model for memory management is defined in the NSObject protocol and the naming convention for standard methods in the combined methods provided in the referenced counting environment. The NSObject class also defines a method: dealloc, which is automatically recycled when an object is called. This article describes the basic rules you need to know in the Cococa program, at the same time, the basic memory management rules and guidelines for many correct use examples are provided. The memory management model is based on object ownership. Any object may have one or more owners. If one object has at least one owner, the object will continue to exist. If one object has no owner, the running system will automatically eliminate it. To ensure that you have an object, Cocoa has formulated the following policies: you have all the objects you have created. You can use "alloc", "new", "copy", or "mutableCopy" (such as alloc, newObject, or mutableCopy) to create an object, you can use "retain" to obtain the ownership of an object. A received object is usually valid when it is received in this method, this method can also safely return the calling program object. You can use "retain" in two ways: (1) When an access method is implemented or the "init" method, to obtain the ownership of an object that you want to store as a property value. (2) when you no longer need an object, you must give up the ownership of the object you own. You can give up the ownership of an object by sending a release message or an autorelease message to it. In Cococa terms, giving up the ownership of an object is usually called a "release" object. You cannot release the ownership of an object that is not owned by you. This is only the inevitable result of the above policy rules and is clearly stipulated.

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.