【问题标题】:How is this NOT a Memory Leak这怎么不是内存泄漏
【发布时间】:2012-05-25 19:36:20
【问题描述】:

我试图弄清楚以下方法如何导致内存泄漏。分配了一个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
    }
}

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

附言我没有启用 ARC。

【问题讨论】:

标签: objective-c ios memory-management


【解决方案1】:

这仍然是内存泄漏!

您必须在您的类中保留对弹出框控制器的引用和/或实现委托方法 popoverControllerDidDismissPopover:(您可以在那里释放它)。
当你调用它的“present...”方法时,popover 控制器不会保留它自己,如果它被释放并且仍然可见,则会引发异常

【讨论】:

    【解决方案2】:

    实现UIPopoverControllerDelegate的

    - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController method and do the following.
    
    - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    
        if(popoverController == yourPopoverController)
    
        {
    
                [popoverController release];
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 2018-07-27
      • 2011-05-31
      • 2015-08-04
      • 2016-08-03
      相关资源
      最近更新 更多