0

Because there are multiple situations in which I would want to pop a view controller from the navigation stack, I have one method that does it and it is called from three different places.

- (void)dismissSelfCon {
    NSLog(@"dismiss");
    [locationManager stopUpdatingHeading];
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
    mapView.delegate = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [[[Trail_TrackerAppDelegate appDelegate] navCon] popViewControllerAnimated:YES];
}

In one situation, if the mapView has an annotation placed on it (I'm not sure if that is the defining characteristic, but I think it is), this method is called (and I am sure that it is called because @"dismiss" is printed to the console), but the location manager does not stop sending location updates! Also, because the delegate is not set to nil, the app crashes because the view controller receives respondsToSelector: from one of the objects of which it is a delegate.

How is this possible?

eric.mitchell
  • 8,817
  • 12
  • 54
  • 92

2 Answers2

0

The most likely cause of this is that locationManager at this point is nil. First rule: always use accessors; don't directly access your ivars except in init and deallloc.

My suspicion from your description would be that this object (the one with dismissSelfCon) doesn't clear locationManager.delegate during dealloc, and that you're being deallocated without calling dismissSelfCon.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

The solution was this:

The way I have my view controller set up (which is a little strange I know, and is something I'm trying to change/fix if you will see my question here: Can't allocate CLLocationManager), the CLLocationManager is being allocated, delegate set, etc in viewDidAppear. I present a MFMessageComposeViewController during the app, and when it gets dismissed, viewDidAppear is called again, re-allocating the CLLocationManager and causing my problem. With a little boolean magic, I adjusted the viewDidAppear code so that the CLLocationManager is only set up and allocated one time.

Community
  • 1
  • 1
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92