Automatic reference counting of Swift learning
- The main learning and research is "memory leaks from leaks" and "strong references resulting from the use of closures" resulting from a direct encounter between the instance object and the instance object.
Note: The automatic reference count is only stored or passed as a reference type, such as classes, closures, enumerations, structs, and so on, which are value stores, and value passing does not have a reference count problem (in fact, we are familiar with the arc in Object-c, and his principle is the same)
- Memory leaks caused by mutual references between instance objects and instance objects
Causes of 1> production
Each of the two classes has an instance variable of the other class as its own property, for example: Create a person class and a Clazz class
Class person { var clazz:clazz?
} class clazz { var Person:person?
} //create two objects, and assigning values to each other causes circular references let jason = person () let class1_1 = Clazz ()
Jason.clazz = class1_1
Class1_1.person = Jason
The above example is an example of a circular reference, Jason->person<->class1_1->clazz.
2> So how do we solve this problem?
In Objective-c we know that a strong reference between this kind and class is the one that declares the attribute with a strong reference and a weak reference, so how does the problem in swift work?
Swift provides two ways: weak references (weak adornments) and non-primary references (unowned adornments)
The rule of thumb: a weak reference may become nil in the life cycle, and once declared it will not become nil with a no-master reference.
In fact, the rule is the same as the objective-c rule, which is also a case of a strong reference with a weak reference or no main reference.
Note: There is a special case where each of these two classes must have a value for each other instance object, so this requires a non-primary reference, and an implicit expansion of the optional attribute
- A memory leak caused by a closed loop reference
1> causes: This is caused by a reference between a closure and a class. The reason for this is that a class takes a closure as its own property, assigns a value to the closure in the class, references itself in the closure, causes a circular reference, and a memory leak
2> ways to solve problems
In Objective-c, our solution is to re-declare the class instance as__weak/__block, the difference between the two modifiers is__weakonly to decorate the object, but__blockcan be decorated with the object and ordinary variables (need to modify the variables in the block)
There is a new concept in Swift: the possession list , in which Swift is to solve the circular reference of a closure by defining a possession list
Possession list: Each element in the possessive list is made up of references to weak or unowned keywords and instances.
So when to use weak: When a reference to possession can sometimes be nil
Unwoned: When possession references and closures are always destroyed at the same time
Automatic reference counting of Swift learning