UIPopoverController 内存泄漏

发布于 2024-11-09 13:29:06 字数 3006 浏览 0 评论 0原文

我有一个通用应用程序,其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-11-16 13:29:06

此行泄漏:

self.settingsPopover = [[UIPopoverController alloc] initWithContentViewController:pvc];

如果您的 settingsPopoverretaincopy 属性,则很可能 retain 。仔细检查,如果确实如此,请像这样自动释放它:

self.settingsPopover = [[[UIPopoverController alloc] initWithContentViewController:pvc] autorelease];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文