In swift, how to get memory back to normal after an SKScene is removed?

冷暖自知 提交于 2019-11-28 23:38:32

Your GameViewController has a strong reference to your GameScene. And your GameScene had a strong reference to your GameViewController. This leads to a strong reference cycle, which means that neither objects will be deallocated.

You need to declare your viewController property in your GameScene as weak.

weak var viewController:GameViewController? = GameViewController()

Using Swift 3, Xcode 8 and iOS 10. After avoiding strong references, taken care of SKTextures, etc. memory level didn't recover after dismissing the scene and returning to the "menu" viewController. I was using:

override func sceneDidLoad() {...}

This is available in iOS 10 but I wanted iOS 8&9 compatibility that's why I've changed to the older:

override func didMove(to view: SKView) {...}

Beside getting compatible with older iOS versions, it turns out that the memory level drops after dismissing the scene. This was a surprise. I'm missing probably some leaks but it's working for me. I hope it helps someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!