2012-01-05 87 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