在加载切片之前退出MapView时的EXC_BAD_ACCESS

时间:2014-07-21 21:24:26

标签: ios objective-c exception mkmapview exc-bad-access

我遇到了一个错误。我在地图中使用OSM图块,因为它们更好地显示了我的位置。所以,我的viewDidLoad是:

[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];
...

好吧,当我尝试按Back或选择当前位置pin(我在其上有一个UIButton,之前所有视图的图块都已加载时)出现错误。

我已经检查了this个问题。并补充说:

- (void)viewWillDisappear:(BOOL)animated {
self.mapView = nil;
[super viewWillDisappear:animated];
}

我尝试在viewWillDisappeardealloc中使用此代码,但似乎都不起作用。接下来,我已经覆盖了后退按钮操作并仍然遇到此问题:

- (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通常会出现,当某些东西试图访问该对象时,当它有一个;就我已经知道已经被释放了。

如何正确解除所有数据?

UPD: 该例外显示在线: return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

它出现在第一个帖子中:

libobjc.A.dylib`objc_msgSend:
0x3b172620:  cbz    r0, 0x3b17265e            ; objc_msgSend + 62
0x3b172622:  ldr.w  r9, [r0]
0x3b172626:  ldrh.w r12, [r9, #12] <<<< Here

最后在UIApplicationMain

 0x3316e148:  blx    0x33731cc4                ; symbol stub for: CFRunLoopSourceSignal$shim
 0x3316e14c:  movs   r0, #0x0 <<< This line

UPD2 : 好吧,在模拟器上运行僵尸并检查堆栈显示了这样的事情:stack

据我所知,当MKTileOverlay尝试在解除分配的MapView上加载切片时,会出现问题。如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

好吧,似乎我找到了解决方案。

原点来自here

我做了两次修复。首先,我将所有切片处理移动到异步块:

dispatch_async(dispatch_get_main_queue(), ^{
    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];});

接下来,我将此行添加到dealloc并删除了viewWillDisappear

- (void)dealloc 
{
    [self.mapView removeOverlays:[self.mapView overlays]];
}

现在它似乎有效。