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