2

I have an app where there is a SpriteKit game, as well as a Main Menu. After clicking the home button, the CPU/Memory don't decrease as they should. I've tried a few things, and the code here is probably redundant, but it hasn't worked for me yet.

Executed whenever the home button is touched:

- (void)goToMainMenu {
    [self performSegueWithIdentifier:@"mainmenu" sender:self];
    [self.spriteView removeFromSuperview];
    self.scene = nil;
    [self.spriteView presentScene:nil];
    self.spriteView = nil;
    self.view = nil;
}

When going back to the game from the main menu, the CPU/Memory only rise.

Tillson
  • 530
  • 2
  • 14

1 Answers1

1

You do not want to segue from home screen to app and to home screen again. You'll have two instances of home screen. And if you segued to the game, again, you'd have two instances of that, too. And every time you follow with circular series of segues, you'll end up with further duplicate instances.

You should either use "unwind" segue (see this answer for example of unwind segue) or use popViewControllerAnimated or dismissViewControllerAnimated depending upon whether you pushed or presented to the game view controller. That's how you return to the original instance of the home screen, rather than creating a new one.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks, but that does not fix the CPU/Memory leak. – Tillson Apr 14 '14 at 01:37
  • @user3477144 If you push/present from `mainmenu` to your game scene, and then push/present from the game scene back to the `mainmenu`, you'll definitely leak (technically, "abandoned" memory). But if you fixed that and are still leaking, then you could have other issues (strong reference cycles, etc.). See [WWDC 2013 video](https://developer.apple.com/videos/wwdc/2013/) "Fixing Memory Issues" or, perhaps even better, [WWDC 2012 video](https://developer.apple.com/videos/wwdc/2012/) "iOS App Performance: Memory". – Rob Apr 14 '14 at 02:07