samedi 25 avril 2015

What's causing this memory leak?


The Problem

I'm currently building an iPad game using SpriteKit. The gameplay is driven by sound provided by EZAudio. After running the Instrumentation tools to profile my app, I noticed that whenever the GameViewController is shown memory allocation jumps up. When I repeatedly show the screen (5+ times) it crashes the app. My project uses ARC.

Navigation

The navigation consists of 4 ViewControllers:

  1. MenuViewController: This shows the menu
  2. CharacterSelectionViewController: It lets you a pick a character to use
  3. GameViewController: This lets you play a game with the player chosen
  4. ScoreViewController: It shows you the score you achieved in the game

1 - MenuViewController

From here you can navigate to CharacterSelectionViewController via a Show (e.g. Push) segue.

2 - CharacterSelectionViewController

You can navigate to GameViewController via a Show (e.g. Push) segue. There is also a back button that goes back to MenuViewController with the following code:

[self.navigationController popViewControllerAnimated:YES];

3 - GameViewController

  1. It first shows a 5 second countdown (using NSTimer)
  2. The game starts with the character chosen in CharacterSelectionViewController
  3. The game can be paused, allowing you to quit and go back to MenuViewController via a manual Show Detail (e.g. Replace) segue.
  4. When the game ends, a manual Show (e.g. Push) segue is called that navigates to the ScoreViewController.

It's view hierarchy consists of three sets of view - one for the countdown, one for the pause menu and one for the game. These are subsequently shown/hidden. See the view hierarchy below:

GameViewController View Hierarchy

4 - ScoreViewController

This allows you to quit or restart the game. When quit it pressed it performs a Show Detail (e.g. Replace) segue to MenuViewController. If restart is pressed it performs an unwind to CharacterSelectionViewController.

Responses

Please provide answers regarding:

  • How this kind of leak could occur
  • Any observations you have from the allocation and leaks screenshots

Allocation

Below, you can see the increasing allocations as I cycle through the apps screens to repeatedly show the GameViewController. I used Mark Generation to be able to show the increase in memory allocations.

Allocations

Leaks

Here you can see the memory leak that occurred. This is ordered by size.

Leaks


Aucun commentaire:

Enregistrer un commentaire