4

I’m having leak problems with GKStateMachine. My App is a pretty straight code to test the problem. This is the GameScene:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

   lazy var gameState:GKStateMachine = GKStateMachine(states: [Introduction(scene: self)])

   override func didMove(to view: SKView) {

      self.gameState.enter(Introduction.self)
   }
} 

And this is my state:

import SpriteKit
import GameplayKit

class Introduction: GKState {

   unowned let scene:GameScene

   init(scene:SKScene) {
      self.scene = scene as! GameScene
      super.init()
   }

   override func didEnter(from previousState: GKState?) {
      print("INSIDE THE Introduction STATE")
   }
}

The problem is that when I run the Leaks debugger, I received one leak as soon as I enter to the state. Does anybody has a suggestion?

iOSTony
  • 165
  • 1
  • 11
  • 1
    Try using `weak var scene:GameScene?` in Introduction class. That removes the leak. Still, I haven't really found the real cause of this. There is a `swift_unknowUnownedInit` call that can be seen in Instruments... And I was not in a mood to look into Swift source to dig what is going on behind the scenes :) Otherwise, your code seems good. If `GameScene` deallocates (say when transitioning to the next scene) both, the scene and introduction instance will be deallocated properly ( Introduction instance doesn't retain the scene due to `unowned` keyword usage). – Whirlwind Jan 10 '17 at 23:44
  • It seem to work. Thank you. Now I am going to try in my original code. The one where I found the problem initially. – iOSTony Jan 11 '17 at 00:12
  • It didn't work on my original code. There is something really strange. According to every website, I am doing everything as it should be. There isn't a definitive solution, though. – iOSTony Jan 11 '17 at 01:54
  • It worked on my original code. I just forgot to make the same adjustment to the other states. I will let you know if everything changes. – iOSTony Jan 11 '17 at 02:16
  • @Fluidity Probably not the same reason every time, but I guess mainly because I don't have time to write a complete answer, or for example I know that question already has an answer, or I am writing from a device... Sometimes I am just lazy lol :) Also i have answered a lot of SpriteKit questions, so I tend not to write things over and over again... Why do you ask ? :) – Whirlwind Jan 12 '17 at 09:26

1 Answers1

-1

You can simplify the constructor to avoid the type cast.

init(scene: GameScene) {
  self.scene = scene
  super.init()
}
Mark Brownsword
  • 2,287
  • 2
  • 14
  • 23
  • How does this actually answers the question? The OP asked about leak... – Whirlwind Jan 10 '17 at 23:29
  • It's just a suggestion since the type cast is unnecessary. – Mark Brownsword Jan 11 '17 at 00:07
  • I know...But that should be a comment ;) Also that init might be even shorter because you can remove super.init(). Since it is the only initializer (a single designated initializer) from a superclass and it has zero arguments, it will be called automatically. – Whirlwind Jan 11 '17 at 00:27
  • 2
    This has no relation with the problem, but in any case, thanks for the suggestion. – iOSTony Jan 11 '17 at 01:56