UIPopoverController and memory management

UIPopoverController *historyPop = [[UIPopoverController alloc] initWithContentViewController:nav]; [nav release]; [historyPop setPopoverContentSize:CGSizeMake(400, 500)]; [historyPop presentPopoverFromRect:CGRectMake(button.frame.origin.x, button.frame.origin.y, button.frame.size.width, 5) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; //[historyPop release]; 

This is my current code, however, Analyzer says it is probably the leak that is (as the release line is commented out). But if I uncomment the release line, the application will shut down and report that dealloc was available on popover while it is still visible, so when exactly should I let go of the popover controller?

+5
memory-management objective-c iphone ipad
source share
2 answers

As mentioned in several places, methods that represent a popover (either from a rectangle or from a button on a toolbar) do not save the popover. So, your view manager should contain a link to it and release it at the appropriate time.

You can do this by setting the view view controller as a popover delegate, as already mentioned. A simpler, albeit slightly less memory efficient approach is to declare a save property for storing the UIPopoverController. When you create a popover, you assign it to a property that saves it. If you later create another popover, it will release the previous popover when reassigning the property. Remember to release the property in the dealloc method of the view view controller (as well as viewDidUnload).

This approach will not proceed, and you do not need to deal with delegates. But you will potentially keep the UIPopoverController object longer than necessary. It is up to you to determine if this is a problem for your application.

+5
source share

Try to implement popover: [historyPop autorelease] . presentPopoverFromRect does not save popover, so autorelease will not work here. You need to set up your class as a delegate of the popover controller and release popover in popoverControllerDidDismissPopover:

+2
source share

All Articles