Automatic reference count in OC

Source: Internet
Author: User

Directory:

1, definition of automatic reference count

2, strong references and weak references

3, analogy manual reference

4, circular reference

5,corefoundation

Content:

Definition of automatic reference count:

(Automatic Reference counting) automatically joins retain and release at compile time, simplifying memory management, automatic reference counting is not memory management, but is through strong references (__strong) and weak references (__ Weak) Pointer to mark how memory is managed, and memory is freed when the number of strong reference pointers to the object equals 0 o'clock.

Strong references and weak references:

__strong, when I create an OC type, the default is strong.

__weak, the pointer is automatically set to nil when the object to which the weak type pointer points is disposed.

Note: When you assign a decorated property, the feature is similar to weak, but its object is not set to nil after it is disposed. There are also retain equivalent to strong, but for manual counting.

Analogy Manual Reference:

In the automatic reference count, I cannot manually invoke Dealloc,retain, release, Autorelease, and Retaincount.

Circular references:

The automatic reference count is not omnipotent, and when a circular reference occurs, the memory is not freed, such as when a property of two types A and b,a is defined to object to B, the attribute in B points to the object of a, and when we complete the relevant logic, the objects A and B are set to nil, then you will find that two objects are not released. , then I should set the properties of one of the classes to Weak,assign or unsafe_unretained.

The relevant control rights are as follows:

  • assign implies __unsafe_unretained ownership.
  • copy implies __strong ownership, as well as the usual behavior of copy semantics on the setter.
  • retain implies __strong ownership.
  • strong implies __strong ownership.
  • unsafe_unretained implies __unsafe_unretained ownership.
  • weak implies __weak ownership.

Corefoundation:

Automatic reference counting can only manage OC objects, but there are other rules for corefoundation objects:

__bridge-oc and CF objects can be converted to each other without affecting the reference, meaning that control is not transferred.

__bridge_retained-oc converted to CF object, CF management memory (cfrelease)

__BRIDGE_TRANSFER-CF to OC Object, OC manages memory

Automatic reference count in OC

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.