CLLocationManager error on release, but why?

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

+4
source share
2 answers

I had the same problem, and perhaps in the depths of the CLLocationManager there was some kind of problem. Fixed:

 [locationManager stopUpdatingLocation]; [self performSelector:@selector(discardLocationManager) onThread:[NSThread currentThread] withObject:nil waitUntilDone:NO]; 

and in discardLocationManager do:

 - (void) discardLocationManager { locationManager.delegate = nil; [locationManager release]; } 
+8
source

You are freeing an instance of CLLocationManager from the callback method, which may not be a good idea.

CLLocationManager calls your locationManager:didUpdateToLocation:fromLocation , etc. If you release the instance of the location manager, you will basically free the object that just called you. Bad idea. This is why the application crashes.

Instead of freeing up an instance of the location manager, you can auto-update it.

Sargon

+3
source

Source: https://habr.com/ru/post/1345009/


All Articles