【问题标题】:MKMapView is not deallocating memory for MKUserLocationMKMapView 没有为 MKUserLocation 释放内存
【发布时间】:2012-02-03 12:23:28
【问题描述】:

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

我已删除所有注释,并在解除分配时禁用了用户位置。这种堆增长的原因可能是什么?

将 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;
}

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];

}

此外,如果我不打开用户位置,也不会出现堆增长。

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

【问题讨论】:

  • VC2 中的 dealloc 真的被调用了吗?
  • 是的,所有其他对象都被释放。我通过为另一个对象取出 dealloc 来检查它,它开始出现在堆中。
  • 能否贴出VC2的dealloc方法和VC1中创建VC2并推送的代码?您是否尝试过在 VC2 的 viewWillDisappear 中关闭用户位置并将地图的委托设置为零?
  • 可能不相关,但您应该在 dealloc 结束时调用 [super dealloc];。在 dealloc 开始时,检查 mapView 是否为 nil。另外,您是在代码中还是在 IB 中创建 mapView?您是否有可能拥有多个地图视图(一个在 IB 中创建,一个在代码中创建)?
  • 我忘了提到这里的 [super dealloc] 更新了代码以反映相同。我在 IB 中创建 mapView,我没有任何代码可以创建另一个 mapView。

标签: ios ipad mkmapview instruments heapshot


【解决方案1】:

如果您在 IB 中创建 MKMapView,您应该在 -viewDidUnload-dealloc 中释放/nil-ing。

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

网络/SO 上似乎有相当多的讨论说这是 5.0.1 之前的 API 错误。您构建的 SDK 版本是什么?

另外,在黑暗中拍摄:仅尝试

self.mapView.showsUserLocation = NO;

self.mapView.showsUserLocation = NO;
[self.mapView.layer removeAllAnimations];
[self.mapView removeAnnotations:self.mapView.annotations];

而不是您现在使用的这些命令的顺序。

可能是您在 MKMapView 有机会正确拆除之前删除了 MKUserLocation 的注释?

【讨论】:

  • 我已经尝试在 viewDidUnload 和 dealloc 中释放它,但我仍然遇到同样的问题。
  • 我尝试了这两种方法,但都没有改变多重分配问题。我使用的是最新的 SDK 5.0.1,我测试它的设备也是如此。
猜你喜欢
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多