Memory management on the array initialized for UITabBarController
<p>I am still learning the ropes of Objective C and iPhone development. My weak point is memory management - any help will be much appreciated.</p>

<p>In the code below, where can I release the <code>NSMutableArray listOfViewControllers</code>? Keep in mind the function <code>createTab</code>s can be called within the app multiple times and the tabs are recreated dynamically based on user input. This function is within a ViewController.</p>

<ol>
<li><p>If i do <code>[listofViewControllers release]</code> just before exiting the function, the app crashes when I have to call createTabs again</p></li>
<li><p>If I use a convenience method like below:</p></li>
</ol>

<p><code>NSMutableArray *listOfViewControllers = [NSMutableArray arrayWithCapacity:1]</code></p>

<p>instead of:</p>

<pre><code>NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init]
</code></pre>

<p>it still crashes when createTabs is called again</p>

<pre><code>-(void) createTabs
{

 //TODO - memory management - where do you release this?
 NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init]; 


 if ([briefingsArray count] &gt; 0)
 {
  //add briefing(s) tab(s)
  for (Briefing *briefing in briefingsArray)
  {

   WebViewController *briefingViewController = [[WebViewController alloc] initWithBriefing: briefing];


   [listOfViewControllers addObject:briefingViewController];

   [briefingViewController release];
  }



  [listOfViewControllers addObject:alertViewController];

  //add archives tab
  NSString *archiveURL = [NSString stringWithFormat: ARCHIVEURL, DeviceID()];

  UIViewController *archiveViewController = [[WebViewController alloc] initWithURL:ARCHIVEURL andTitle:@"Archives" andImage:@"archive_icon.png"];
  [listOfViewControllers addObject:archiveViewController];

  [archiveViewController release];


 }


 NSArray *oldlistOfViewControllers = [self.tabBarController viewControllers];
 UIViewController *vcOld = [oldlistOfViewControllers objectAtIndex:[oldlistOfViewControllers count] -1];


 [listOfViewControllers addObject:vcOld];


 [self.tabBarController setViewControllers:listOfViewControllers
                                  animated:YES];


}
</code></pre>
