4
\$\begingroup\$

Using ARC, I'm calling the following function in an iOS app every time the app gets opened in the applicationDidBecomeActive function.

My concern is that it could create a memory leak because it's creating new instances of UIViewControllers every time the app gets opened from the background.

- (void)showMainWindow{
  NSLog(@"@Info @AppDelegate: Showing Main Window");
  self.leftMenuViewController = nil;
  self.rightMenuViewController = nil;
  MenuViewController *tempLeft = [[MenuViewController alloc]initWithSide:@"left"];
  MenuViewController *tempRight = [[MenuViewController alloc]initWithSide:@"right"];
  self.leftMenuViewController = tempLeft;
  self.rightMenuViewController = tempRight;
  self.viewDeckController =  [[IIViewDeckController alloc] initWithCenterViewController:self.webViewController
                                                                                leftViewController:self.leftMenuViewController
                                                                               rightViewController:self.rightMenuViewController];

  self.window.rootViewController = self.viewDeckController;
  [[NSNotificationCenter defaultCenter] postNotificationName:@"updateNavigationBar" object:nil];
}
\$\endgroup\$
1
  • \$\begingroup\$ I don't think so, though it might depend on if the view deck controller retains the web view controller. simple way to check though: run in instruments \$\endgroup\$
    – wattson12
    Commented Nov 15, 2013 at 9:49

1 Answer 1

4
\$\begingroup\$

This code doesn't appear to leak. To be sure, however, you should use the leaks instrument to periodically check your code.

To do such,

  1. Select Product -> Profile (or, ⌘ I)
  2. Choose `Leaks`
  3. Select Profile
  4. Exercise your app (i.e. go through all the options, background it, etc)

For more information on using the leaks tool, you can checkout Ray Wenderlich's Instruments Tutorial for iOS: How to Debug Memory Leaks.

Additional Notes:

1) Unless you have a really good reason to, you shouldn't be setting self.window.rootViewController every time that the app returns to the foreground. Instead of calling showMainWindow from within applicationDidBecomeActive, you should probably move this code within application:didFinishLaunchingWithOptions:, which is the more common place to do initial app setup.

Further, if you need a controller to know when the app has entered the foreground (I imagine that this is the reason you're actually doing this), you should have it register to receive notification of UIApplicationWillEnterForegroundNotification. I.e.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(YOUR_SELECTOR)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];

    // ... whatever else you do here...
}

// Also make sure to unregister too
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

2) To make this code a bit more cleaner and easier to read, I would rewrite it like this:

- (void)showMainWindow 
{

    NSLog(@"@Info @AppDelegate: Showing Main Window");

    self.leftMenuViewController = [[MenuViewController alloc]initWithSide:@"left"];;
    self.rightMenuViewController = [[MenuViewController alloc]initWithSide:@"right"];;

    self.viewDeckController =  [[IIViewDeckController alloc] initWithCenterViewController:self.webViewController
                                                                                leftViewController:self.leftMenuViewController
                                                                           rightViewController:self.rightMenuViewController];

  self.window.rootViewController = self.viewDeckController;
  [[NSNotificationCenter defaultCenter] postNotificationName:@"updateNavigationBar" object:nil];
}

In such, you don't have to explicitly set self.leftMenuViewController and self.rightMenuViewController to nil or create the temp objects.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.