CLLocationManager初始化,释放,初始化然后释放导致崩溃?

时间:2010-06-25 18:00:14

标签: iphone cocoa-touch iphone-sdk-3.0 ios4

我有一个用例,应用程序将自动尝试检索位置,用户可以拒绝该权限,然后用户可以触发应用程序再次查找该位置(这次允许它),然后应用程序会崩溃。下面是基本代码和用例步骤,我做错了什么?

@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. 用户与用户界面互动,触发调用(IBAction) UserTriggerToGetLocation
  7. FindLocation
  8. 电话再次请求权限,此时用户允许
  9. locationManager:didUpdateToLocation:fromLocation做其事
  10. 当调用locationManager:didUpdateToLocation:fromLocation时,应用程序在[locationManager release]内崩溃。具体来说,我得到的EXC_BAD_ACCESS暗示locationManager已被释放?但在哪里?

    我做错了什么?

1 个答案:

答案 0 :(得分:0)

Guh,没关系。我认为我dealloc之前发布错误但我也意识到我不需要在此之前发布。只需在其响应处理程序中停止locationManager,我就可以通过将UserTriggerToGetLocation更改为再次调用[locationManager startUpdatingLocation] NOT FindLocation来重新启动它。