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
已经发布?但是哪里?
我做错了什么?