We will use the following knowledge during development:
Differences between copy and retain
Copy: Create an object with an index count of 1 and release the old object.
Retain: Release the old object, assign the value of the old object to the input object, and increase the index count of the input object to 1.
What is the above damn meaning?
Copy actually creates the same object, and retain is not:
For example, an NSString object with the address 0 × 1111 and the content @ "STR"
After copying data to another NSString, the address is 0 × 2222, the content is the same, the new object retain is 1, and the old object does not change
After retain reaches another NSString, the address is the same (create a pointer and copy the pointer), and the content is of course the same. The retain value of this object is + 1
That is to say, retain is a pointer copy and copy is a content copy. Wow, it's much easier than you think...
Release objects by mistake
Question 1:
Value = [array objectAtIndex: n]; // obtain the object in an array.
[Arry removeObjectAtIndex: n]; // unload the object
Because the value obtains the object, but another owner release the object, the value is now a swing pointer (invalid data)
Question 2:
MyArray = [NSArray array];
...
[MyArray release];
NSArray returns an automatically released object. Not only should myArray not be release after a period of time, but should be retained when appropriate to prevent the array from being accidentally released by the system.
Question 3:
Rocket = [rocketLauncher aRocket];
[RocketLauncher release];
Like the Data Collection class object like array, if we get a sub-object of a class without retrying it, this rocket will actually lose its meaning when the original parent class is released.
Autorelease in different memory management environments of Cocoa
H hybrid memory management environment: Garbage Collection + Reference Counting)
Although hybrid environments are not recommended in most cases, autorelease requires the following considerations:
Garbage collection in a hybrid environment: the drain method should be used, because release is meaningless in GC mode.
In the index counting environment: drain and release have the same effect on autoreleasepool
Misunderstanding of autorelease www.2cto.com
A Cocoa memory management is divided into index Counting (Reference Counting/Retain Count) and Garbage Collection (Garbage Collection ). The iPhone currently only supports the former, so autorelease has become a "shortcut" for many people ".
But! Autorelease is not "automatically released". Unlike the garbage collection method, the relationship between objects is detected and spam is deleted. However, autorelease is actually "delayed release". After a running cycle is marked as autorelease, it will be released.
Remember to use autorelease with caution and understand autorelease to prevent the object from being released by the system when you still need it.
Memory Management issues involved in Interface Builder
Key points:
If a variable is defined as an IBOutlet in the class, you do not need to instantiate it. The xib loader initializes it.
If a variable is defined as an IBOutlet in the class, you must release it. The xib loader won't help... ...
* Do not initialize two times. The memory overflows and an error occurs when the object is locked.
Reference Counting
* Retain value = index count (Reference Counting)
The NSArray object will retain (retain value plus one) any object in the array. When NSArray is detached (dealloc), all objects in the array are released once (the retain value is reduced by one ). Not only is it NSArray, but any Collection class performs similar operations. For example, NSDictionary or even UINavigationController.
The index count of the objects created by Alloc/init is 1. You do not need to retain it again.
[NSArray array], [NSDate date], and other "methods" create an object with an index count of 1, but it is also an automatically released object. So it is a local temporary object, so it doesn't matter. If it is a variable (iVar) intended to be used in the whole Class, you must retain it.
The "Auto Release" method is executed for the return values of the default class methods. (* NSArray in the preceding example)
In the unload method "dealloc" in the class, release all NS objects that are not balanced. (* All records that are not autorelisted and whose retain value is 1)
NSString Memory Management
Example:
AString = @ "I am a string that 2 years old, man! ";
In this case, strings are stored and managed by the system.
AString = [NSString stringWithFormat: @ "I am a string that % d years old, man! ", 2];
In the second case, we need to go to the retain and release strings, regardless of the system.
Objective-C Memory Management
1. You initialize (alloc/init) the object, and you need to release (release) it. For example:
NSMutableArray aArray = [[NSArray alloc] init];
After, you need
[AArray release];
2. You need to release retain or copy. For example:
[AArray retain]
After, you need
[AArray release];
3. the objects to be passed (assign), retain and release you need to consider. For example:
Obj2 = [[obj1 someMethod] autorelease];
When object 2 receives an automatically released value of object 1 or transmits a basic data type (NSInteger, NSString): You may want to retain object 2, to prevent it from being automatically released before being used. However, after retain, it is necessary to release it when appropriate.
Why can't I directly call dealloc instead of release?
Dealloc is not equal to free in C. dealloc does not release the memory or reduce the index count (Reference counting. Therefore, calling dealloc directly fails to release the memory.
In Objective-C, index counting plays a decisive role.
From Bai Lu. China