Reputation: 21
I'm making an app that changes between many XIBs. Although Leaks tool doesn't call it out, whenever I load a new view, the program's memory footprint gets larger and larger.
If I comment out:
[self.view addSubview:currentView.view];
the program's memory use doesn't change.
Relevant code:
- (void)setPage{
[currentView release]; //retained
switch (pageNumber) {
case 0:
currentView = [[TheViewController alloc]
initWithNibName:@"PageX"
bundle:nil];
break;
///Page 1, Page2, etc.
}
for (clearingView in [self.view subviews]) {
[clearingView removeFromSuperview];
//[clearingView release]; = crash
}
[self.view addSubview:currentView.view]; //Leaky
}
I'm using one view controller.
Upvotes: 1
Views: 907
Reputation: 21883
There is nothing obvious I can spot in the code, but the naming is a little confusing and do you really need to retain currentView. If you are using currentView somewhere else it could be that by doing so you are adding to the retain count which would cause a leak.
Here's what I would be inclined to do (Note; I'm typing this on the fly so it may not be 100% correct!):
- (void)setPage{
// Presuming you only want one subview at a time.
[[[self.view subViews] objectAtIndex:0] removeFromSuperview];
UIViewController * newController = nil;
switch (pageNumber) {
case 0:
newController = [[TheViewController alloc]
initWithNibName:@"PageX"
bundle:nil];
break;
///Page 1, Page2, etc.
}
[self.view addSubview:newController.view];
[newController release];
}
I don't think you need a class variable (currentView) and this is nicely contained within the method.
Upvotes: 0
Reputation: 4825
When you add a subview, its retain count goes up. I don't see where you are removing the currentView. If you have retained it previously, and then release it in this function, that won't remove it from the view.
Try putting a [currentView removeFromSuperview]; as the first line and see if that helps.
- (void) setPage {
[currentView.view removeFromSuperview];
[currentView release];
switch (pageNumber) {
case 0:
currentView = [[TheViewController alloc] initWithNibName:@"PageX" bundle:nil];
break;
}
for (clearingView in [self.view subviews])
[clearingView removeFromSuperview];
[self.view addSubview:currentView.view];
}
Edit 1: Actually, I see that you are removing currentView along with all other views in the for loop. That being the case, I really don't see any issue with your code at all. The only thing I can guess is that somehow the retain count of your ViewController or View is getting bumped up some where else.
When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.
Upvotes: 1