Hi all, thought I'd relate this story since I saw, whilst googling for the answer, a lot of questions but no solutions.
Say you've got a UINavigationController wrapped around a UITableViewController. If you created the table view controller from within Xcode, you've got a whole bunch of useful code already put there, including this gem:
Here's the problem: when the user navigates away from this newly created view controller, it is not released. This becomes a serious problem if, like me, you have allocated something large, like an AudioQueue. (In my case, it was more annoying that the AudioQueue's stop and dispose functions were not being called in my dealloc method).
SO. What's the deal? Well, the problem is, even though you release the view controller, the navigation controller retains it as well; hence, when you hit back, it's still got a retain count of 1. So, what you need to do is this:
This adds it to the autorelease pool created when you pushed that view controller, so it gets released properly.
NOTE: This means you have to watch your memory management carefully! Make sure you release everything you retained, etc. I actually had to do some of this in viewDidDisappear: because of the situation with AudioQueues/NSTimers and threading/run loops.
Say you've got a UINavigationController wrapped around a UITableViewController. If you created the table view controller from within Xcode, you've got a whole bunch of useful code already put there, including this gem:
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController];
// [anotherViewController release];
Here's the problem: when the user navigates away from this newly created view controller, it is not released. This becomes a serious problem if, like me, you have allocated something large, like an AudioQueue. (In my case, it was more annoying that the AudioQueue's stop and dispose functions were not being called in my dealloc method).
SO. What's the deal? Well, the problem is, even though you release the view controller, the navigation controller retains it as well; hence, when you hit back, it's still got a retain count of 1. So, what you need to do is this:
Code:
AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
[self.navigationController pushViewController:anotherViewController];
[anotherViewController release];
[anotherViewController autorelease]; // <-----
This adds it to the autorelease pool created when you pushed that view controller, so it gets released properly.
NOTE: This means you have to watch your memory management carefully! Make sure you release everything you retained, etc. I actually had to do some of this in viewDidDisappear: because of the situation with AudioQueues/NSTimers and threading/run loops.