MKMapView不会为MKUserLocation释放内存

Kri*_*a K 4 instruments mkmapview ipad ios heapshot

我有一个导航控制器,其中VC1将VC2推送到导航堆栈.VC2在基于选项卡的视图中有一个MKMapView,用户位置已打开.当我使用Heapshot Analysis工具检查仪器的重复分配时,我重复发现当我回到VC1时,一些MKUserLocation对象没有被释放. 在此输入图像描述

我删除了所有注释,并在dealloc时禁用了用户位置.这种堆增长的原因是什么?

将VC2推送到导航堆栈的VC1代码:

- (NSIndexPath *)tableView:(UITableView *)tableView 
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VC2 *vc2 = [[VC2 alloc] init];
    vc2.index = indexPath.row;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[self navigationController] pushViewController:vc2 
                                           animated:YES];
    [vc2 release];
    vc2 = nil;
    return nil;
}
Run Code Online (Sandbox Code Playgroud)

VC2中的dealloc代码:

- (void)dealloc {
//Other UILabel, UITextField objects dealloc methods go here
//The next four lines of code do not make a difference even if they are in viewWillDisappear
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView.layer removeAllAnimations];
self.mapView.showsUserLocation = NO;//This line does not make a difference in heapshot
mapView.delegate = nil;
[mapView release];
mapView = nil;
[super dealloc];
Run Code Online (Sandbox Code Playgroud)

}

此外,如果我不打开用户位置,则没有堆增长.

更新:我已经在模拟器和iPad 3G + WiFi上对此进行了测试,我发现这两种情况下的堆增长.

Ben*_*her 5

如果你MKMapView在IB 中创建,你应该nil-viewDidUnloadAND中释放它/ -dealloc.

如果不这样做,如果在视图控制器的生命周期内卸载/重新加载视图,则所有视图元素(无论如何都设置为保留属性)将在重新加载时泄漏.

在网络/ SO上似乎有相当数量的喋喋不休,说这是5.0.0之前的API错误.您要构建哪个版本的SDK?

此外,在黑暗中拍摄:只尝试

self.mapView.showsUserLocation = NO;
Run Code Online (Sandbox Code Playgroud)

要么

self.mapView.showsUserLocation = NO;
[self.mapView.layer removeAllAnimations];
[self.mapView removeAnnotations:self.mapView.annotations];
Run Code Online (Sandbox Code Playgroud)

而不是你现在使用的这些命令的顺序.

可能是你删除MKUserLocation之前的注释MKMapView有可能正确地拆掉它吗?