2012-05-25 54 views
2

我想弄清楚如何使用下面的方法而不是导致内存泄漏。 A UIPopoverController已分配,但如果我包含autoreleaserelease调用,则应用程序崩溃,并显示消息'-[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 
    } 
} 

这似乎违背了基本的内存管理实践。

p.s.我没有启用ARC。

+1

的可能重复[保管好UIPopoverController,UIActionSheet和莫代尔/释放模式视图控制器?](http://stackoverflow.com/questions/2867709/retain-release-pattern-for-uipopovercontroller-uiactionsheet-and-modal-view-co) – StilesCrisis

+0

好问题,但我认为这是由上面的链接回答。 – StilesCrisis

+0

@StilesCrisis你说得对,但是当我搜索时没有出现。 –

回答

3

这仍然是一个内存泄漏!

你必须在你的类中保留对popover控制器的引用和/或实现委托方法popoverControllerDidDismissPopover :(你可以在那里发布它)。
一酥料饼的控制器不保留本身,当你调用它的“礼物...” - 方法并抛出一个异常,如果它被释放,仍然可见

1

Implemment UIPopoverControllerDelegate的

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

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

    if(popoverController == yourPopoverController) 

    { 

      [popoverController release]; 

    } 

}