We have discussed how to effectively control "object ownership and reference count" in the memory management of the iPhone software. In this article, we will introduce "Auto release) convenience method ).
BKJIA recommendation topic: iPhone application development
Sometimes when an object is created by an owner, the pointer of the object is immediately passed to other owners. At this time, the Creator does not want to own this object any more, but if a release message is sent to it immediately, this object will be released immediately-so that other owners have not yet had time to retain this object. The solution to this dilemma is to send an autorelease message to the object: The Creator no longer owns the object; the object becomes an automatically released object, but will not be immediately released; other owners can retain or copy the object for time and become its unique owner.
Let's take a look at the example code listing 3-1 for automatic release ). An owner first uses the alloc method to create an object. At this time, the owner owns this object and the reference count of the object is 1. Then, the owner Automatically releases the object. The owner has abandoned the ownership, but the reference count of the object is still 1 for a period of time. We can see another benefit of automatic release: You will not cause memory leakage because you forget to send a release message to the object later.
Code List 3-1
-
-
- -(Object*)returnAutoreleaseObject {
-
- Object* obj = [[Object alloc] init];
-
- return [obj autorelease];
-
- }
There is a major constructor method related to automatic release. The objects constructed by them are directly released. This kind of constructor method is called a convenient method. For example, the string in the following sentence is an automatically released object, and stringWithFormat is a convenient method.
- NSString* string = [NSString stringWithFormat:@”autoreleaseString”];
Let's take a few examples of convenient methods to facilitate your future development.
1. NSArray's arrayWithObjects: And arrayWithArray :.
2. imageNamed: Of the UIImage :.
3. numberWithBool of NSNumber.
Now we have explained that the autorelease method will release an object after a period of time, during which we can safely use this object. So how long is this time? We need to learn more about the automatic release mechanism before answering this question.
Let's take a look at the automatic release pool. The Auto Release pool is an instance of the NSAID utoreleasepool, which contains the object that receives the autorelease message. When an automatic release pool is destroyed by dealloc), it will send a release message to each object in the pool. If you send an autorelease message to an object multiple times, when the Auto release pool is destroyed, this object will also receive the same number of release messages ). It can be seen that an automatically released object can survive at least when the automatically released pool is destroyed.
When will the automatic release pool be created and destroyed? At the beginning of each event cycle, the system automatically creates an automatic release pool. At the end of each event cycle, the system automatically destroys the automatic release pool. Generally, you can understand that when your code continues to run, the automatically released pool will not be destroyed, during this period, you can also safely use the automatically released objects. When your code runs and starts waiting for user input or other events, the automatically released pool will be released, and objects in the pool will receive a release message, and some may be destroyed.
So far, I believe you have a general understanding of the automatic release mechanism. Automatic release instead of direct release can help you save some code and increase development speed. However, it has a direct disadvantage: It delays the release of objects, and will occupy a large amount of memory resources when there are a large number of automatically released objects. Therefore, you need to avoid releasing a large number of objects automatically. In addition, in the following two cases, you need to manually create and manually destroy the automatic release pool:
1. When other threads are enabled outside the main thread: the system will automatically generate and destroy the automatic release pool in the main thread.
2. When you create a large number of automatically released objects in a short period of time: timely destruction helps to effectively use the limited memory resources on the iPad.