What is the most correct way to add a UIViewController to a subview and remove it? <p>I'm trying to add a UIViewController subview, and then upon clicking a button close it. My current code does the job, I'm just not sure if it will leak or cause any problems.</p> <p>So first I add the subview</p> <pre><code>-(IBAction)openNewView:(id)sender{ // start animation [UIView beginAnimations:@"CurlUp" context:nil]; [UIView setAnimationDuration:.3]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; // add the view newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]]; [self.view addSubview:newVC.view]; [UIView commitAnimations]; } </code></pre> <p>Then in newViewController.m I have the function to remove it</p> <pre><code>-(IBAction)closeNewView:(id)sender{ // start animation [UIView beginAnimations:@"curldown" context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:.3]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES]; // close dialog [self.view removeFromSuperview]; [UIView commitAnimations]; [self.view release]; } </code></pre> <p>Like I said this works, however when I Analyze the code, it tells me:</p> <p>Potential leak of an object allocated on line X and stored into 'newViewController' for:</p> <pre><code>newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]]; </code></pre> <p>and</p> <p>Incorrect decrement of the reference count of an object that is not owned at this point by the caller for <code>[self.view release];</code></p> <p>If I <code>autorelease</code> the viewController instead of <code>[self.view release]</code> it crashes upon removing (also if I release the view after adding it) with: -[FirstViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0xd21c7e0</p> <p>If I call <code>[newVC release]</code> in either viewControllers <code>dealloc</code> it fails to build.</p> <p>Hopefully I'm not asking a fairly obvious question, but what is the correct way to add and remove viewControllers?</p>