Arc Auto Reference count

Source: Internet
Author: User

Start the automatic reference counting option.

Select the project's properties file--"Search automatic Reference--" Objective-c automatic Reference Counting--"Yes

The difference between ARC and manual memory management.

ARC is not the GC that determines whether the reference count is 0 in the run, which clears the memory. Instead, the memory management code is automatically generated by the static analysis tool analyze before the code is compiled.

After opening the arc, you can no longer use the Retain series manual memory tube method, may override the Dealloc method but cannot again the method [Super Dealloc]

The Core Foundation stuff still needs to be manually release, the object that starts with CF. Because CF is not in the range of arc.

Before arc is enabled
  
 
  1. @implementation NonARCObject
  2. -(id)initWithName:(NSString *)newName {
  3. self = [super init];
  4. if (self) {
  5. [name release] ;
  6. name = [newName retain];
  7. }
  8. return self;
  9. }
  10. -(void)dealloc {
  11. [name release];
  12. [super dealloc];
  13. }
  14. @end
After ARC is enabled
  
 
  1. @implementation NonARCObject
  2. -(id)initWithName:(NSString *)newName {
  3. if (self) {
  4. // 编译器自动加入 [name release]; name = [newName retain];
  5. name = newName;
  6. }
  7. return self;
  8. }
  9. @end
Strong references and weak references

Strong references and if referenced concepts exist in the ARC environment.

Strong references

A strong reference refers to a reference that causes the object to reference counter +1. Similar to retain.
Objects are cleared when all strong references are fully release.

    • __strong keyword to define strong references
    • @property (strong) to represent strong reference properties.
Weak references

A weak reference does not cause the object to reference a reference to counter +1. Similar to Assgin, the direct pointer is assigned a value.

    • __weak keyword to define weak references
    • @property (week) to indicate if a property is referenced.
When do I use a reference?

Parent-child references are often present in the ARC environment.

The person class has a child class object pointer.
The child class has a person class object pointer.

If a person references a child pointer that is a strong reference, and the person class that the child refers to is also a strong reference, it will cause an issue where the object cannot be freed.
1.Person class release to release child
The 2.Child class releases the person again.
A parent-child relationship is a strong reference, and a circular reference cannot be released when it is released.

The subclass contains a reference to the parent class, if it is resolved by reference.

    1. The person class strongly references the child class.
    2. The child class weakly references the person class.

When the person class is released to release the child, the child finds itself weak to refer to a person who does not clean up the person and only sets the person's reference to nil;

If the referenced object is cleaned, the system automatically sets the reference to nil, so if the reference does not show the zombie pointer

Zombie pointer: A pointer to a piece of memory that has been cleaned out.

Arc Auto Reference count

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.