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.