0
我有一個用例,其中應用程序會自動嘗試檢索一個位置,用戶可以拒絕該權限,然後用戶可以觸發應用程序再次查找該位置(這次允許它),但然後該應用程序將崩潰。以下是基本代碼和用例步驟,下面是我做錯了什麼?CLLocationManager初始化,釋放,初始化,然後釋放導致崩潰?
@interface AppViewController : UIViewController <CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
@property (retain,nonatomic) CLLocationManager *locationManager;
//... method declaration
@end
@implementation AppViewController
@synthesize locationManager;
-(void)MethodThatAutomaticallyGetsLocation{
[self FindLocation];
}
-(IBAction)UserTriggerToGetLocation{
[self FindLocation];
}
-(void)FindLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
// ... do some stuff
// ... save location info to core data object
[locationManager stopUpdatingLocation];
locationManager.delegate = nil;
[locationManager release];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
// ... conditionally display error message
// based on type and app state
[locationManager stopUpdatingLocation];
locationManager.delegate = nil;
[locationManager release];
}
- (void)dealloc {
// locationManager not released here, its released above
}
@end
- 應用程序加載瀏覽,郵件
MethodThatAutomaticallyGetsLocation
FindLocation
被稱爲設置locationManager
- 電話詢問權限共享位置
- 用戶拒絕允許
locationManager:didFailWithError
被調用時,釋放locationManager
- 用戶與UI交互,觸發
(IBAction) UserTriggerToGetLocation
這就要求FindLocation
- 電話又問許可,這時候用戶允許它
locationManager:didUpdateToLocation:fromLocation
就執行
然後應用程序崩潰內locationManager:didUpdateToLocation:fromLocation
時[locationManager release]
被調用。具體來說,我得到EXC_BAD_ACCESS
這意味着locationManager
已經發布?但是哪裏?
我做錯了什麼?