IOS Basics: Retain,copy,assign and Autorelease__ios

Source: Internet
Author: User
Tags garbage collection

Http://www.cnblogs.com/martin1009/archive/2012/06/18/2553184.html

Oneretain, copy, assign difference

1. Suppose you allocate a chunk of memory with malloc and assign it to pointer a, and then you want pointer b to share that memory, so you assign a to (assign) B. At this point A and B points to the same memory, ask when a no longer need this block of memory, can directly release it. The answer is no, because a does not know whether B is still using this block of memory, if A is released, then B in the use of this memory will cause the program crash off.

2. Understand the problem of the assign in 1, then how to solve it. The simplest method is to use the reference count (reference counting), or the example above, where we set a reference count for that block of memory, and when the memory is allocated and assigned to a, the reference count is 1. The reference count increases to 2 when assigning a value 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. The above two points is actually the difference between assign and retain, assign is directly assigned, which may cause problems in 1, when the data for the original type int, float, you can use assign. Retain as described in 2, using the reference count, retain caused the reference count plus 1, release caused the reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called, memory is reclaimed.

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 compiler-generated getter and setter are atomic operations. In a multithreaded environment, atomic operations are necessary, or they can cause incorrect results. The addition of the Atomic,setter function will become the following:

If (property!= newvalue) {
[Property release];
property = [NewValue retain];
}

Second, a deeper understanding (including autorelease)

1. Count plus one after retain. After Alloc count is 1,release, it will invoke Dealloc to destroy the object.
If retain, you need to release two times. It is usually necessary to assign parameters to member variables in method retain.
For example:
ClassA has setname this method:
-(void) SetName: (ClassName *) inputname
{
name = InputName;
[Name retain]; Here Retian, equal 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 Dealloc to really free up 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 have no relationship.
Autorelease principle:
A. Establish a autorelease pool first
B. Objects are generated from this autorelease pool.
C. Call the Autorelease function after the object is generated, the function of which is simply to mark the Autorelease pool and let the pool remember to release the object in the future.
D. At the end of the procedure, the pool itself needs to be rerlease, at which point the pool will release each object marked as Autorelease. If an object retain count is greater than 1 at this time, the object is 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 it is not possible to release name in ClassA Dealloc, otherwise the release pool will release this retain count 0 object.

Remember: If the object is Alloc or new, you need to call release. If you use Autorelease, release only once retain (let retain count is always 1).

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.