applicationDidBecomeActive: Am I causing a memory leak? 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];
}
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,


Select Product -> Profile (or, ⌘ I)
Choose `Leaks`
Select Profile
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.