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.