One, retain, copy, assign difference
1. Suppose you allocate a piece of memory with malloc and assign its address to pointer a, and then you want pointer b to share the memory, so you assign a value to (assign) B. At this time A and B point to the same piece of memory, I ask when a no longer need this memory, can you directly release it? The answer is no, because a does not know whether B is still using this memory, and if A is released, then B will cause the program to crash when it uses this memory.
2. Understand the problem of assign in 1, then how to solve? The simplest method is to use a reference count (reference counting), or the above example, we set a reference count for that memory, and when the memory is assigned and assigned to a, the reference count is 1. The reference count increases to 2 when a is assigned to B. If a no longer uses this memory, it only needs to subtract the reference count by 1, indicating that it no longer owns the memory. b The reference count is also reduced by 1 when the memory is no longer used. When the reference count becomes 0, the memory is no longer referenced by any pointers, and the system can release it directly.
3. Above two points is actually the difference between assign and retain, assign is directly assigned, which may cause 1 problems, when the data is int, float and other native types, you can use assign. Retain as described in 2, using a reference count, retain causes a reference count plus 1, release causes a reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called and memory is recycled.
4. Copy is used when you do not want a and B to share a piece of memory. A and B each have their own memory.
5. Atomic and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations. In a multithreaded environment, atomic operations are necessary, otherwise they may cause incorrect results. Adding the Atomic,setter function will change to the following:
if (Property! = newvalue) {
[Property release];
property = [NewValue retain];
}
Second, in-depth understanding (including autorelease)
1. After retain Count plus one. Alloc after the count is 1,release will call Dealloc destroy this object.
If retain, you need to release two times. It is usually necessary to assign a parameter to a member variable in method retain.
For example:
ClassA has setname this method:
-(void) SetName: (ClassName *) inputname
{
name = InputName;
[Name retain]; Here Retian, equivalent to [InputName retain],count equals 2
}
When called:
ClassName *myname = [[ClassName alloc] init];
[ClassA Setname:myname]; Retain count = = 2
[MyName release]; Retain Count==1, release name in ClassA's dealloc to really free memory.
2. Autorelease is more tricky and easily confused by its name. I want to emphasize here: Autorelease is not garbage collection, completely different from the GC in Java or. Net.
Autorelease and scopes do not have any relationship!
Autorelease principle:
A. Create a autorelease pool first
B. The object is generated from within this autorelease pool.
C. After the object is generated, call the Autorelease function, which simply marks the Autorelease pool and lets the pool remember to release the object in the future.
D. At the end of the program, the pool itself needs to be rerlease, and the pool will release each object marked as Autorelease once. If an object retain count is greater than 1 at this point, the object is still not destroyed.
The above example should be written like this:
ClassName *myname = [[[ClassName alloc] init] autorelease];//marked as Autorelease
[ClassA Setname:myname]; Retain count = = 2
[MyName release]; Retain Count==1, note that in ClassA Dealloc cannot release name, otherwise the release pool will release this retain count 0 object, this is not right.
Remember one point: If this object is your alloc or new, you need to call release. If you use Autorelease, release only once the retain has occurred (let retain count is always 1).