开发者

UIPopoverController and release in delegate-method; "potential memory leak"

开发者 https://www.devze.com 2023-03-08 10:20 出处:网络
I\'m doing an alloc/init with my UIPopoverController, then release it in the delegate method. Whenever I perform a \"build and analyze\", I get memory warnings with \"potential leaks\" - am I doing so

I'm doing an alloc/init with my UIPopoverController, then release it in the delegate method. Whenever I perform a "build and analyze", I get memory warnings with "potential leaks" - am I doing something wrong or is there a way to get rid of those warnings?

Thanks a lot!

- (void) somewhere {
    M开发者_高级运维yViewController *vc = [[MyViewController alloc] init];
    UIPopoverController *popover=[[UIPopoverController alloc] initWithContentViewController:vc];
    [vc release];

    // show the popover
    [popover presentPopoverFromRect:[cell frame] inView:self.tableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    popover.delegate = self;
}

- (void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
   [popoverController release];
   popoverController = nil;
}

EDIT: show complete somewhere-function


First of all, in the popoverControllerDidDismissPopover: method you should not release the popoverController instance, since it will be released by the framework.

Second, what's the meaning of that somewhere method? You should post the complete implementation here ... I suppose you are showing up the popover view, so you should do something like this:

- (void) somewhere {

    // I suppose you have a local variable to hold the controller
    _myPopover = [[UIPopoverController alloc]   initWithContentViewController:vc];
    _myPopover .delegate = self;

    // ... do other things to configure the popover, if necessary

    // I suppose you show it, with something like this
    [_myPopover presentPopoverFromRect:yourRect inView:yourView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
}

Then, when you no longer need the popover controller, you can release it. You could try with your code, this way:

- (void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    [_myPopover release];
    _myPopover = nil;
}

This way you will always hold the instance of the popover where you need it, and release it when you have finished with it. In your previous implementation that instance was not bound to anything.

0

精彩评论

暂无评论...
验证码 换一张
取 消