Hello,
first of all – thank you for 2D iOS and tvOS Games by Tutorials book – it is really helpfull
So I created a small game with several scenes and facing some memory issues:
-
The first issue I already solved – a scene was not deallocated after transition to another scene. I didn’t have any strong references inside of this scene, but the
deinit
was simply not called at all. I present a scene like in the tutorial:if let scene = GameScene(fileNamed:"GameScene") { let transition = SKTransition.crossFadeWithDuration(1.0) scene.scaleMode = .AspectFill scene.backgroundColor = SKColor.redColor() self.view?.presentScene(scene, transition: transition) }
So I solved this by overriding
willMoveFromView
. In this method I iterate through all nodes and remove they from parent node. I also stop all actions and remove them. Finally I callremoveFromParent()
method. After this thedeinit
method is called when I present the next scene. This approach was not obvious for me, because it is not used in the tutorials and there are no issues with memory usage. -
Though all scenes and nodes are being deallocated now (I just added
debugPrint("\(NSStringFromClass(self.dynamicType)).deinit")
indeinit
method of each custom class to double check whetherdeinit
is called), I still have a memory issue when I simply switch between two scenes (simply scenes with 3-4 sprite nodes) and each scene presents the next one like mentioned above:
Even when I simply stay in one scene the memory usage increases:
So I tried to find memory leaks with Instruments and there are some of them (at this point I simply display a start scene with couple of buttons and two basic animations):
It seems like the problem is in MetalKit, but I don’t know how to fix this and which part of my game causes this issue.
Do you have any advices where I can start searching?
Thanks