UIPopoverController 内存泄漏
我有一个通用应用程序,其 iPad 部分使用 UIPopoverController,它显示与 iPhone 部分共享的 UIViewController。当iPhone版本加载和释放视图控制器时,一切都很好(没有内存泄漏)。当 iPad 版本加载它时,两个项目,一个 NSDictionary 和一个 NSArray 泄漏。
在视图控制器中,创建并释放了两个对象,因此
- (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];
}
iPad 版本创建并解除了 UIPopoverController,因此
- (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;
}
我不确定为什么 NSDictionary 和 NSArray 在 iPad 上泄漏,但在 iPhone 上却没有。 Profiler 表示它们的保留计数最初设置为 1,但从未释放。
谢谢!
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.
In 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此行泄漏:
如果您的
settingsPopover
是retain
或copy
属性,则很可能retain
。仔细检查,如果确实如此,请像这样自动释放它:This line leaks:
if your
settingsPopover
is aretain
orcopy
property,retain
being very likely. Double check that, if that's indeed the case, autorelease it like so: