IOS development-retain/assign/strong/weak/copy/mutablecopy/autorelease

Source: Internet
Author: User

Readwrite: it is a readable and writable feature. The getter and setter methods must be generated;
Readonly is the feature of the capital. Only the getter method is generated, and the setter method is not generated. You do not want to use the attribute when it is changed outside the class;
The reference count after alloc object allocation is 1 retain object reference count + 1

Copy an object to a new object (new memory address). The reference count is 1. The original object count remains unchanged.

Assign: it is a value assignment feature. The setter method assigns input parameters to instance variables (a key, both in and out) for basic data types;
Weak: the attribute of the object variable introduced by ARC, which has one more function than assign. When the object disappears, the pointer is set to nil, avoiding the wild pointer (not a null pointer, is a pointer to the "junk" memory (unavailable memory );
Retain: indicates that the property is held. The setter method will keep the input parameters first, and then assign values (two keys, both in and out). The input parameter retaincount plus 1;
Strong: introduced by ARC, equivalent to retain;
Copy: indicates the value assignment feature. The setter method copies the input object. When a new object needs to be completely created (two houses and two keys );
Nonatomic: Non-atomic operations determine whether the setter and getter methods generated by the compiler are atomic operations without synchronization. multi-threaded access improves performance,
_ Unsafe_unretain: the object reference will not be added with 1. After the object is released, it will not be set to nil, which may lead to a wild pointer and should be used as little as possible.
Autorelease: object reference count-1 if it is 0, it will not be released immediately. It will be released in recent pools.


Member variables and attributes

The actual situation is not as simple as above. You may need to call the variables allocated by another function in one function. At this time, you have two options: class member variables and usage attributes.

@ Interface TestMem: NSObject {
TestObject * m_testObject; // member variable
TestObject * testObject; // member variable
}
The member variables are consistent with the memory management above, but they must maintain a balance between the addition and subtraction of the reference count in different functions. Therefore, you must check whether the previous allocation has been made every time you allocate the variable. Whether it can still be called
When to use attributes?
1. Make the member public.
2. outlet is generally declared as an attribute (this memory is controlled by the system, but we should do the same operation, which will be discussed later)
3. If many functions need to change this object, or this function will trigger many times, we recommend that you use attributes. Let's see what the property function looks like after it is expanded:


// assign-(void)setTestObject :(id)newValue{testObject= newValue;}// retain-(void)setTestObject :(id)newValue{if (testObject!= newValue) {[testObject release];testObject= [newValue retain];}}// copy-(void)setTestObject :(id)newValue{if (testObject != newValue) {[testObject release];testObject = [newValue copy];}}


Asssign is equivalent to pointer assignment, and does not operate on the reference count. Note that the original object is no longer used and must be set to nil.
Retain is equivalent to adding 1 to the reference count of the original object
Copy does not change the reference count of the original object. The reference count of a new object is 1.
Note:
The left value of self. testObject calls the setTestObject method. The right value is the get method. The get method is simple and needless to say.
TestObject uses a member variable.
Self. testObject = [[testObject alloc] init]; // error reatin twice
TestObject = [NSArray objectbyindex: 0]; // The error is not safe. If there is no retain, the release will fail.
If testObject already has a value, it will also mem leak

Automatic Object Management IOS provides many static (+) class methods for creating objects. These methods are static and can be directly called by class names.
For example:
NSString * testString = [NSString stringWithFormat: @ "test"];
TestString is an automatically managed object. You do not need to relese it. It has a large retain count, and the number remains unchanged after release.


Exceptions Some objects generated through alloc are automatically managed as follows:
NSString * testString = [[NSString alloc] initWithString: @ "test1"];
Retain count is also a large number, and there is no way to release
However, for code correspondence, you should add [testString release];

Otherwise, xcode Analyze will recognize the memory leak, but the Instruments leak tool does not.



On the way to learning, I will share with you

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.