【发布时间】: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