我有一个用例,应用程序将自动尝试检索位置,用户可以拒绝该权限,然后用户可以触发应用程序再次查找该位置(这次允许它),然后应用程序会崩溃。下面是基本代码和用例步骤,我做错了什么?
@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
(IBAction) UserTriggerToGetLocation
FindLocation
locationManager:didUpdateToLocation:fromLocation
做其事当调用locationManager:didUpdateToLocation:fromLocation
时,应用程序在[locationManager release]
内崩溃。具体来说,我得到的EXC_BAD_ACCESS
暗示locationManager
已被释放?但在哪里?
我做错了什么?
答案 0 :(得分:0)
Guh,没关系。我认为我我在dealloc
之前发布错误但我也意识到我不需要在此之前发布。只需在其响应处理程序中停止locationManager,我就可以通过将UserTriggerToGetLocation
更改为再次调用[locationManager startUpdatingLocation]
NOT FindLocation
来重新启动它。