2012-01-05 84 views
4

我有一个导航控制器,其中VC1将VC2压入导航堆栈。 VC2在基于标签的视图中有一个MKMapView,用户位置打开。当我使用Heapshot Analysis工具检查仪器重复分配时,当我回到VC1时,我反复查找一些MKUserLocation对象没有被释放。 enter image description hereMKMapView并没有为MKUserLocation释放内存

我已经删除了所有注释,并在dealloc时禁用了用户位置。这可能是堆增长的原因?

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

的的dealloc代码在VC2:

是推动VC2到导航堆栈

VC1代码

- (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上测试了这一点,我发现这两种情况下堆增长。

+0

VC2中的dealloc真的被调用了吗? – Anna 2012-01-05 17:29:02

+0

是的,所有其他对象都被释放。我通过取出另一个对象的dealloc来检查它,并且它开始出现在heapshot中。 – 2012-01-05 17:54:17

+0

你可以发布VC2 dealloc方法和创建VC2的VC1中的代码并推送它吗?您是否尝试过关闭用户位置,并将地图的代理设置为无VC2的viewWillDisappear? – Anna 2012-01-05 18:08:07

回答

5

如果要在IB中创建MKMapView,则应该发布/ nil -in它在-viewDidUnload-dealloc中。

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

Web/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的注释?

+0

我试着在viewDidUnload和dealloc中释放它,但我仍然有同样的问题。 – 2012-01-17 15:19:22

+0

我尝试了两种方法,但都没有改变多重分配问题。我正在使用最新的SDK 5.0.1,我测试过的设备也是如此。 – 2012-01-18 21:36:50