I have a universal app whose iPad portion uses a UIPopoverController which displays a UIViewController shared with the iPhone portion. When the iPhone version loads and releases the view controller, all is fine (no memory leak). When the iPad version loads it, two items, a NSDictionary and an NSArray leak.
开发者_如何学JAVAIn the view controller, the two objects are created and dealloc thus
- (void)setupModels {
fonts = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Baskerville", @"Baskerville",
@"Georgia", @"Georgia",
@"HelveticaNeue", @"Helvetica Neue",
@"Palatino-Roman", @"Palatino Roman",
@"Verdana", @"Verdana", nil];
fontNameKeys = [[NSArray alloc] initWithObjects:
@"Baskerville",
@"Georgia",
@"Helvetica Neue",
@"Palatino Roman",
@"Verdana", nil];
}
- (void)dealloc {
[fonts release], fonts=nil;
[fontNameKeys release], fontNameKeys=nil;
[super dealloc];
}
The iPad version creates and dismisses the UIPopoverController thus
- (void)displaySettingsPopover:(id)sender {
if([self.settingsPopover isPopoverVisible]) {
//close the popover view if toolbar button was touched again and popover is already visible
//Thanks to @chrisonhismac
[self.settingsPopover dismissPopoverAnimated:YES];
[self.settingsPopover.delegate popoverControllerDidDismissPopover:self.settingsPopover];
} else {
if (!self.settingsPopover) {
//build our custom popover view
PreferencesViewController_iPhone *pvc = [[PreferencesViewController_iPhone alloc] initWithNibName:nil
bundle:nil
callbackObject:self
selector:@selector(applySettingsFromPopover)];
[pvc.view setBackgroundColor:[UIColor lightGrayColor]];
[pvc.navigationItem setTitle:@"Preferences"];
[pvc setContentSizeForViewInPopover:CGSizeMake(320, 444)];
//create a popover controller
self.settingsPopover = [[UIPopoverController alloc] initWithContentViewController:pvc];
[pvc release];
self.settingsPopover.delegate = self;
}
//present the popover view non-modal with a
//refrence to the toolbar button which was pressed
[self.settingsPopover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
[self.settingsPopover setDelegate:nil];
[settingsPopover release];
settingsPopover=nil;
}
I'm not sure why the NSDictionary and the NSArray are leaking for the iPad but not the iPhone. The Profiler says their retain count initially sets to 1 but is never dealloc.
Thanks!
This line leaks:
self.settingsPopover = [[UIPopoverController alloc] initWithContentViewController:pvc];
if your settingsPopover
is a retain
or copy
property, retain
being very likely. Double check that, if that's indeed the case, autorelease it like so:
self.settingsPopover = [[[UIPopoverController alloc] initWithContentViewController:pvc] autorelease];
精彩评论