Search code examples
objective-cios7memory-leakspresentviewcontroller

Memory buildup with presentViewController


I have a button click handler that saves an object and then presents another controller. My problem is that with each click the memory allocated increases.

  if (success) {
      ALRollsViewController *rollsController = [[UIStoryboard storyboardWithName:@"Entry" bundle:nil]instantiateViewControllerWithIdentifier:@"RollsController"];
      rollsController.camera= selectedCamera;
      [self presentViewController:rollsController
                         animated:YES
                       completion:nil];
  }

If I use dismissViewControllerAnimated as opposed to presentViewController:rollsController there is no buildup. Do I need to release the instantiated controller somehow?


Solution

  • What do you expect? Each click instanticates a new instance of UIStoryboard. Naming convention here is, that a method beginning with the name of the object (name without prefixes) returns a newly created instance of the object.

    See and compare to NSArray arrayWith... or NSString stringWith...

    Plus you need an instance of the view controller every time to be presented. Both instances are kept until the view controller is dismissed. (I am not 100% positive about the UIStoryboard instance to be kept that long, but the newly presented view controller will eat up your heap and a bit of your stack.)