When it frees itself when the view manager is rejected

I have a question regarding freeing memory and locking / closing.

Below is the Swift method

self!.dismissViewControllerAnimated(false, completion: { println(self); }) 

Or objective method C

  [self dismissViewControllerAnimated:NO completion:^{ NSLog("%@",self); }]; 

I would really appreciate it if someone could explain when the self will be released in the method described above. Is this after starting the completion block or before? I understand that he took care of ARC, but I would like to know if self will receive a release message in the completion block or after that. Therefore, if I do a little cleaning in the completion block (access to myself), is it safe / acceptable or not?

+6
source share
2 answers

There are really two separate questions to fully understand the answer:

1. When are the variables indicated in the closed closure issue released?

You can think of closures as another type (they really are in Swift). Each closure creates strong ownership of the variables referenced within it, including self . This ensures that you never reference a variable that has been freed. Just like in any other object, when a closure is released, it releases its ownership of all its variables.

If a closure is the only thing with a strong reference to this variable, the variable will be canceled when the closure is canceled.

In short, variables remain in memory while the closure is still in memory.

2. When are faults released?

Now the second part of this is important for understanding when the closure closes. You can save the closure in a variable:

 func myMethod() { var myClosure = { println(self) } myClosure() } 

In this case, the closure has a strong reference from its myClosure variable, and the closure has a strong reference to self . As soon as myClosure goes out of scope, i.e. When myMethod completes, it will be canceled. When this happens, he will be released.

You may also have a situation, for example, in your question, when you pass a closure to another method. In this case, the method by which you are closing closes a strong link to your closing. When this method exits, it will release your closure, the closure will be freed, and it will release all the variables captured inside it.

An exception

Sometimes it is necessary to define a closure in which it does not obtain ownership of the variable. You will do this to avoid circular links . You can read more in the Apple documentation about what a circular link is and how to prevent it from happening here . However, it is important to understand that if you don’t put in an explicit effort (and code), closures will always capture strong references to the variables referenced within it.

+4
source

In Swift, by developing / debugging and trying to understand the timing of operations, you can add the following view controllers (or any class) to track when or when an instance will be released.

Although this does not help you define the "strong reference loops" described here:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html

 deinit { println(__FUNCTION__, "\(self)") } 
+2
source

Source: https://habr.com/ru/post/972677/


All Articles