简体   繁体   中英

iphone app crashes when location services are toggled

I have an app which uses location services. in my appdelegate i am initializing my location manager property

if(self.locationManager == nil)
    {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];  // allocate cllocationmanager property
        self.locationManager.delegate = self;  // confirm the delegate 
        [self.locationManager startUpdatingLocation];  // start updating for location 
    }

and the location manager delegate is made nil in dealloc method

- (void)dealloc
{
   [_window release];
   [_viewController release];

 // [locationManager release];
   self.locationManager.delegate = nil;
   [super dealloc];
}

the app crashes 1 out of 10 times when i toggle location services from settings

After debugging in instruments , this is the error displayed

在此处输入图片说明

Kindly suggest the fix for the same .

create a instance variable if you already haven't that is the same name as your cllocationmanager property and only call self.locationmanager when you are setting as shown here

   if (nil == locationManager){
        self.locationManager = [[CLLocationManager alloc] init];
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.delegate = self;
        [locationManager startUpdatingLocation];
        startLocation = nil;
    }

this is written for arc so add your autorelease if needed.

您尚未告诉locationManager stopUpdatingLocation - Apple文档

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM