UIPopoverController内存泄漏

时间:2011-05-25 05:20:59

标签: ipad ios4 nsarray nsdictionary 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,但绝不是dealloc。

谢谢!

1 个答案:

答案 0 :(得分:4)

此行泄露:

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

如果您的settingsPopover属于retaincopy属性,retain极有可能。仔细检查,如果确实如此,请自动释放它:

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