2010-06-25 94 views
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 
  1. 应用程序加载浏览,邮件MethodThatAutomaticallyGetsLocation
  2. FindLocation被称为设置locationManager
  3. 电话询问权限共享位置
  4. 用户拒绝允许
  5. locationManager:didFailWithError被调用时,释放locationManager
  6. 用户与UI交互,触发(IBAction) UserTriggerToGetLocation这就要求FindLocation
  7. 电话又问许可,这时候用户允许它
  8. locationManager:didUpdateToLocation:fromLocation就执行

然后应用程序崩溃内locationManager:didUpdateToLocation:fromLocation[locationManager release]被调用。具体来说,我得到EXC_BAD_ACCESS这意味着locationManager已经发布?但是哪里?

我做错了什么?

回答

0

呃,没关系。我想我amdealloc之前发布时发生了一些问题,但我也意识到我不需要在此之前发布。通过简单地在其响应处理程序中停止locationManager,我可以通过将UserTriggerToGetLocation更改为[locationManager startUpdatingLocation] NOT FindLocation来重新启动它以执行步骤6。