0

In my iOS7 app I have a singleton called ICVModel. When the app starts this singleton gets initialized and creates CLLocationManager which immediately starts updating location. This works fine, CLLocationManager is a strong property in ICVModel. I have implemented didUpdateLocations: to always save last location to ICVModel's strong property on this singleton.

My problem is that when the app goes to background and than becomes active again, i always get EXC_BAD_ACCESS in didUpdateLocations:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [ICVModel sharedSingleton].lastLocation = [locations lastObject]; //EXC_BAD_ACCESS
}

Also my app uses region based notifications, so isn't it possible that in this case this method is called when the app is in background (and ICVModel is deallocated?)?

I have no idea why... Thanks a lot!

animal_chin
  • 6,610
  • 9
  • 37
  • 41
  • This may help you http://stackoverflow.com/questions/327082/exc-bad-access-signal-received – Muddu Patil Mar 25 '14 at 09:29
  • More than likely one of these is nil, can you put an NSLog in front of the line of code to display their contents – Flexicoder Mar 25 '14 at 09:30
  • I think something wrong in your singleton realization, but for sure set a exception breakpoint and see backtrace – sage444 Mar 25 '14 at 09:32
  • well enabling zombies and trying to see what caused it lead to nothing in this case... i understand what exc_bad_access is and why it is caused, but i dont know why it was fired in this case :) – animal_chin Mar 25 '14 at 09:33
  • So are any of them nil? – Flexicoder Mar 25 '14 at 09:37
  • added nslog before setting last location... they both seems OK... [locations lastObject] : <+50.10202375,+14.44871465> +/- 65.00m (speed -1.00 mps / course -1.00) @ 25.03.14 10:37:41 last location : <+50.10205235,+14.44871580> +/- 50.00m (speed 0.00 mps / course -1.00) @ 25.03.14 10:34:56 – animal_chin Mar 25 '14 at 09:39
  • i have also added a breakpoint to see all the properties before the signal is fired and everything is fine...ICVModel is allocated, new location ok... really weird... – animal_chin Mar 25 '14 at 09:49

1 Answers1

0

Fixed it. The problem was in something else. In one ViewController i added a observer for this lastLocation property change and didn't removed it...when the app went to background, VC was deallocated (i did it manually) but the singleton was still running and was trying to call observeValueForKeyPath:ofObject: on deallocated object (-> EXC_BAD_ACCESS).

Basically this caused the problem (in some VC) :

[[ICVModel sharedSingleton] addObserver:self
                                 forKeyPath:@"lastLocation"
                                    options:NSKeyValueObservingOptionNew
                                    context:NULL];

and this fixed it :

- (void)viewDidDisappear:(BOOL)animated {
    [[ICVModel sharedSingleton] removeObserver:self
                                    forKeyPath:@"lastLocation"];
}

Thanks everyone for help and hopefully this will help at least someone...

animal_chin
  • 6,610
  • 9
  • 37
  • 41