Whi*_*ind 7 memory-leaks instruments ios sprite-kit swift
我只是在玩泄漏并试图故意创造一个.所以,即使做这样的事情也很愚蠢:
class LeakingObjectA{
var strongRefToB:LeakingObjectB?
deinit{ print("LeakingObjectA deinit")}
}
class LeakingObjectB{
var strongRefToA:LeakingObjectA?
deinit{ print("LeakingObjectB deinit")}
}
Run Code Online (Sandbox Code Playgroud)
这对于科学目的来说很好,这创造了一个强大的参考周期.
现在didMoveToView我在内部声明局部常量并像这样发生泄漏:
override func didMoveToView(view: SKView) {
let a = LeakingObjectA()
let b = LeakingObjectB()
a.strongRefToB = b
b.strongRefToA = a
}
Run Code Online (Sandbox Code Playgroud)
另一个场景转换后,现场的deinit正确调用,但是deinits从a和b实例并不实际调用.
另外我说泄漏,因为这实际上是在仪器中检测到泄漏:
现在,如果我将这两个本地变量声明为场景的属性,则仪器检测到泄漏之间存在差异:
class GameScene:SKScene {
let a = LeakingObjectA()
let b = LeakingObjectB()
//...later in didMoveToView method I make a strong reference cycle like from the example above
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,当然,现场的deinit被转换后调用的欢迎,并与上述相同,deinits从a和b实例不叫(因为很强的参考周期).
尽管如此,仪器中没有检测到泄漏......那么对此有什么合理的解释呢?
我无法使用下面的代码复制此内容。临时场景已正确取消初始化,并且两者LeakingObjectA都LeakingObjectB显示在泄漏工具中。
class LeakingObjectA {
var strongRefToB:LeakingObjectB?
deinit{ print("LeakingObjectA deinit")}
}
class LeakingObjectB {
var strongRefToA:LeakingObjectA?
deinit{ print("LeakingObjectB deinit")}
}
class TempScene : SKScene {
let a = LeakingObjectA()
let b = LeakingObjectB()
override init() {
a.strongRefToB = b
b.strongRefToA = a
super.init(size: CGSizeZero)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
print("Deiniting temp scene")
}
}
class ViewController {
var tempScene: TempScene?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tempScene = TempScene()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
tempScene = nil
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
178 次 |
| 最近记录: |