RGM*_*GML 6 closures memory-leaks strong-references retain-cycle swift
任何人都可以解释为什么这不泄漏?
我正在捕获self,closure所以我会有两个强指针指向对方,因此,deinit永远不应该为Person对象调用该消息.
首先,这是我的班级人物:
class Person {
var name: String
init(name: String) { self.name = name }
deinit { print("\(name) is being deinitialized") }
}
Run Code Online (Sandbox Code Playgroud)
这是我的ViewController的实现:
class ViewController: UIViewController {
var john:Person?
func callClosureFunction( closure:(name:Bool) -> () ) {
closure(name: true)
}
override func viewDidLoad() {
super.viewDidLoad()
john = Person(name:"John")
self.callClosureFunction { (name) in
self.john?.name = "John Appleseed"
self.john = nil
// xcode prints - John Appleseed is being deinitialized
}
}
}
Run Code Online (Sandbox Code Playgroud)
我期待能够通过以下方式解决问题:
self.callClosureFunction { [weak self] (name) in ...
Run Code Online (Sandbox Code Playgroud)
但这甚至不是必要的.为什么?
由于您的视图控制器未保留闭合,因此没有循环引用。如果您编写此代码:
class ViewController: UIViewController {
var john:Person?
var closure:(Bool)->()?
func callClosureFunction( closure:((name:Bool) -> ())? ) {
closure?(name: true)
}
override func viewDidLoad() {
super.viewDidLoad()
john = Person(name:"John")
closure = { (name) in
self.john?.name = "John Appleseed"
// Because this closure will never be released, the instance of Person will never deinit either
}
self.callClosureFunction(closure)
}
}
Run Code Online (Sandbox Code Playgroud)
然后,视图控制器将保留闭包,而闭包将通过对的引用保留视图控制器self。因此,两者都不会被释放,并且如果您未显式设置self.john = nil(您在原始示例中进行了设置),则Person实例将永远不会被deninit调用。
self在不必要的情况下,不适当地使用弱闭包是很普遍的(这实际上会导致一些晦涩的错误)。要记住的关键规则是,弱引用通常不是ARC中的默认设置。除非会导致保留周期,否则应将默认值设置为强,在这种情况下,应仅将弱用于打破该循环引用。同为封:强self应该是默认的,除非在self这种情况下也具有较强的参考封闭自己。
| 归档时间: |
|
| 查看次数: |
543 次 |
| 最近记录: |