4

I've been creating a game via SpriteKit and am using an SKScene with SKSpriteNodes and SkNodes in order to do so. When the user dies, I have the parent View Controller of the SKScene perform a segue to a game over View Controller. I remove some of the SKSpriteNodes, but none of the SKNodes from the game scene. When the user presses play again, it performs a segue back to the View Controller with the SKScene and the game will start over. I was wondering if it is necessary for me to remove the SKScene and it's nodes from the memory after the user dies and before I segue to the game over View Controller. Are all of the SKNodes and SKSpriteNodes that I created still being held in the memory somewhere? I'm asking this question due to how I noticed that after every game/match the memory for my application being displayed in Xcode and the Instruments application is higher than it was before the match/game.

*If it's relevant to the issue, I have an Admob banner and rewarded video ad loaded and displayed to the user on the game over View Controller.

Gabe
  • 168
  • 11
  • 2
    Not an answer to your question, but take a look at this concept in compare to what you are doing currently : https://stackoverflow.com/a/35409586/3402095 As you said, you have to use Instruments (Leaks) to find out where are the leaks. Then, you can show the relevant code, so we can tell you how to prevent from strong retain cycles. – Whirlwind Jun 28 '17 at 07:58
  • Thanks, my method of transitioning between scenes and their parent View Controllers and other View Controllers was actually the reason for the application's memory usage issues. I was performing show segues between View Controllers where unwind segues 'should' have been and some View Controllers were not calling deinit so they were staying in the memory rather than being removed. – Gabe Jun 28 '17 at 17:37

1 Answers1

3

They will be held in memory so long as there is a strong reference to them.

Without the code we won't know for sure, but a quick way you can check is to add this to your gamescene and then perform the segues:

deinit {
  print("gamescene go byebye!")
}

Regardless, this shouldn't be an issue for small games memory-wise. If you have games with a lot of SKScenes, then you would need to ensure that they are going to bed properly.

As a supplement to the answer, here is a quick tutorial on Swift memory management, spotting memory leaks, retain cycles, and what to do about them:

https://www.raywenderlich.com/134411/arc-memory-management-swift

Fluidity
  • 3,985
  • 1
  • 13
  • 34
  • The use of deinit and the article helped me solve the problem! I realized that rather than performing an unwind segue to return to the View Controller with the SKScene I was performing a 'show' segue so another instance of the SKScene/View Controller was being created and loaded into the memory. The Game Over VC was also not de-initializing correct and as a result, it would stay in the memory as well. I fiixed up these problems and now a much lower reading is being displayed by Instruments. Plus, my device is no longer heating up from the game so thats a good sign :-) ! Thanks for the help!! – Gabe Jun 28 '17 at 17:34