This may be one of those stupid questions when, as soon as the solution is indicated, you feel pretty stupid, wondering how you did not see it, but I cannot understand why this part of my application crashes with EXC_BAD_ACCESS (and not stack trace).
I have a CLLocationManager * locationManager (ivar declared in an interface file) that is created on viewDidLoad if locationServices is enabled:
- (void)viewDidLoad { [super viewDidLoad]; if ([CLLocationManager locationServicesEnabled]) [self findUserLocation]; ... } #pragma mark - Location finder methods - (void)findUserLocation { locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers; [locationManager startUpdatingLocation]; }
So, the location manager starts updating the location, and every time the update is found, the delegate method is called, where I check whether I need a timeout or continue searching for my desired function:
#pragma mark - CLLocationManager delegates - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if ([newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp] > 8) [self locationManagerTimeOut]; else if ((newLocation.horizontalAccuracy <= manager.desiredAccuracy) && (newLocation.verticalAccuracy <= manager.desiredAccuracy)) [self locationManagerLockedPosition]; }
If the position is locked, this method is called:
- (void)locationManagerLockedPosition { [locationManager stopUpdatingLocation]; locationManager.delegate = nil; [locationManager release], locationManager = nil; NSLog (@"add results to view"); }
If time runs out, this is the method:
- (void)locationManagerTimeOut { [locationManager stopUpdatingLocation]; locationManager.delegate = nil; [locationManager release], locationManager = nil; NSLog (@"Time out!"); }
The problem is that in any case (timeout or blocked position) I get NSLog output in the console, and then 2 seconds after the application crashes?
I wonder if I comment on my line [locationManager release]... everything works fine, but WHY? Also, if I move the [locationManager release] method to my dealloc , none of them will work!
Did I miss something basic here?
Thanks!
Horn