2

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.

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • 1
    possible duplicate of [Retain/release pattern for UIPopoverController, UIActionSheet, and modal view controllers?](http://stackoverflow.com/questions/2867709/retain-release-pattern-for-uipopovercontroller-uiactionsheet-and-modal-view-co) – StilesCrisis May 25 '12 at 19:41
  • Good question, but I think it's answered by the link above. – StilesCrisis May 25 '12 at 19:42
  • @StilesCrisis You're right, but it did not show up when I was searching. – Mike D May 25 '12 at 19:44

2 Answers2

3

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

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
1

Implemment UIPopoverControllerDelegate's

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController method and do the following.

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {

    if(popoverController == yourPopoverController)

    {

            [popoverController release];

    }

}
CodaFi
  • 43,043
  • 8
  • 107
  • 153
Charith Nidarsha
  • 4,195
  • 3
  • 28
  • 32