2011-09-21 50 views
3

釋放是否有任何潛在的內存問題,用下面的代碼?:iOS的內存問題,在viewDidLoad中初始化和viewDidUnload

- (void)viewDidLoad 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
} 

- (void)viewWillAppear:(BOOL)animated { 

    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
} 

- (void)viewDidUnload 
{ 
    [locationManager release]; 
    locationManager=nil; 
    [super viewDidUnload]; 
} 

我與儀器檢查,它說有內存上面的代碼泄漏。

回答

5

您應該釋放的LocationManager在dealloc方法。

- (void)dealloc 
{ 
    [locationManager release]; 
    [super dealloc]; 
} 

原因是viewDidUnload不保證被調用。

詳細信息請參考以下這些問題:

When is UIViewController viewDidUnload called?

viewdidunload is not getting called at all!

+0

你是正確的...我與儀器檢查,並有一個內存上面的代碼我張貼..泄露這就是爲什麼我問這個問題......它似乎與dealloc工作正常.. – MomentH

0

它看起來相當不錯,除了:

  1. 截至viewDidLoad開頭添加[super viewDidLoad];
  2. 截至viewWillAppear:開頭添加[super viewWillAppear:animated];