I met across one bug. I use OSM tiles in my map because they display my location better. So, my viewDidLoad
is:
[super viewDidLoad];
self.mapView.delegate = self;
self.searchBar.delegate = self;
self.title = @"Select Location";
NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
overlay.canReplaceMapContent = YES;
[self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];
self.locationManager = [[CLLocationManager alloc] init];
if ( [CLLocationManager locationServicesEnabled] ) {
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 1000;
[self.locationManager startUpdatingLocation];
}
self.geocoder = [[CLGeocoder alloc] init];
...
Well, the bug appears, when I try to press Back or select the current location pin (I have a UIButton on it, before all the tiles for the view are loaded.
I've checked this question. And added this:
- (void)viewWillDisappear:(BOOL)animated {
self.mapView = nil;
[super viewWillDisappear:animated];
}
I tried to use this code in both viewWillDisappear
and dealloc
, but neither seem to work. Next, I've overridden back button action and still having this issue:
- (void)viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
self.locationManager.delegate = nil;
self.mapView = nil;
self.searchBar.delegate = nil;
[self.navigationController popViewControllerAnimated:YES];
}
[super viewWillDisappear:animated];
}
EXC_BAD_ACCESS usually appears, when something tries to access the object, when it has a;ready been deallocated, as far as I know.
How can I properly dealloc all the data?
UPD:
The exception appears on line:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
It appears in the first thread:
libobjc.A.dylib`objc_msgSend:
0x3b172620: cbz r0, 0x3b17265e ; objc_msgSend + 62
0x3b172622: ldr.w r9, [r0]
0x3b172626: ldrh.w r12, [r9, #12] <<<< Here
And finally here in UIApplicationMain
0x3316e148: blx 0x33731cc4 ; symbol stub for: CFRunLoopSourceSignal$shim
0x3316e14c: movs r0, #0x0 <<< This line
UPD2:
Well, running with zombies on simulator and examining stack showed such things:
As far as I understand, the problem appears, when MKTileOverlay tries to load tiles on deallocated MapView. How this can be fixed?