I am trying to figure out how the following method does not cause a memory leak. A UIPopoverController is allocated, yet, if I include an autorelease or release call, the application crashes, with the message '-[UIPopoverController dealloc] reached while popover is still visible.'.
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
[mapView deselectAnnotation:view.annotation animated:TRUE];
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
UIViewController *con = [[UIViewController alloc] init];
UIPopoverController *poc = [[UIPopoverController alloc] initWithContentViewController:con];
[con release];
poc.popoverContentSize = CGSizeMake( 320, 320 );
[poc presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:UIPopoverArrowDirectionAny animated:TRUE];
}
else {
; // TODO (miked): display stuff another way
}
}
This seems to go against basic memory management practices.
p.s. I have not enabled ARC.
This still is a memory leak!
You have to keep a reference to the popover controller in your class and/or implement the delegate method popoverControllerDidDismissPopover: (you can release it there).
A popover controller does not retain itself when you call its “present…”-methods and throws an exception if it is deallocated and still visible