C. Greene
C. Greene

Reputation: 281

Ios popToRootViewControllerAnimated causing Memory Issues

I am making my SpriteKit game. When the player dies, my goal is to have the game transition back to the start screen. This is accomplished by the code below. However, I notice that the memory increases each time a new game begins. Xcode Instruments is not showing a memory leak. When the memory reaches roughly 150mb the games frame rate drops and the game become unplayable.

In the GameScene I call this function when the player dies

func gameOver(){
    if let block = gameOverBlock {
        worldNode.removeAllChildren()
        worldNode.removeAllActions()
        worldNode.removeFromParent()
        self.removeAllChildren()
        block()
    }
}

Back in the GameViewController the following functions get called

     scene!.gameOverBlock = {
        [weak self] in
        self!.goBack()
    }
}

func goBack(){
    scene!.removeFromParent()
    navigationController!.popToRootViewControllerAnimated(false)
    return
}

If anyone has any ideas as to how I can accomplish this without a memory leak, it would much be appreciated.

Upvotes: 2

Views: 104

Answers (1)

C. Greene
C. Greene

Reputation: 281

After commenting out tons of code, I have found the problem. The methods that I have posted above were not causing the leak, as Matthew suggested, there was a strong reference in the middle of my code that was stopping the ARC from releasing memory. Ill post the problem code incase anyone else may have a similar problem.

In my GameViewController, I had the following block:

  scene!.zoomInBlock = {
       self.scene!.size = CGSizeMake(self.scene!.size.width / 2, self.scene!.size.height / 2)
    }

The correct way (without causing a strong reference) to write this would be:

  scene!.zoomInBlock = {
     [unowned self] in self.scene!.size = CGSizeMake(self.scene!.size.width / 2, self.scene!.size.height / 2)
    }

Upvotes: 2

Related Questions