地理围栏 iOS 崩溃 EXC_BAD_ACCESS
Posted
技术标签:
【中文标题】地理围栏 iOS 崩溃 EXC_BAD_ACCESS【英文标题】:Geofence iOS Crash EXC_BAD_ACCESS 【发布时间】:2013-02-21 05:20:41 【问题描述】:我正在尝试在我的应用中实施地理围栏。我正在编写Kevin McMahon 的精彩教程。
我的问题是每次尝试创建地理围栏时都会收到“EXC_BAD_ACCESS (code=1, address= ...)。
这是我的代码:
NSMutableArray *placeID = [[NSMutableArray alloc] init];
NSMutableArray *lats = [[NSMutableArray alloc] init];
NSMutableArray *longs = [[NSMutableArray alloc] init];
for (int i = 0; i < [responseObject count]; i++)
[placeID addObject:[[responseObject objectAtIndex:i]objectForKey:@"id"]];
[longs addObject:[[responseObject objectAtIndex:i]objectForKey:@"long"]];
[lats addObject:[[responseObject objectAtIndex:i]objectForKey:@"lat"]];
NSMutableDictionary *latDict = [[NSMutableDictionary alloc] initWithObjects:lats forKeys:placeID];
NSMutableDictionary *longDict = [[NSMutableDictionary alloc] initWithObjects:longs forKeys:placeID];
NSMutableArray *geofences = [[NSMutableArray array] init];
for(int k = 0; k < [responseObject count]; k++)
CLRegion *geofence = [self mapDictionaryToRegionWithLat:latDict withLong:longDict usingID:[placeID objectAtIndex:k]];
[geofences addObject:geofence];
[self initializeRegionMonitoring:geofences];
创建 CLRegion
- (CLRegion*)mapDictionaryToRegionWithLat:(NSDictionary *)latDict withLong:(NSDictionary *)longDict usingID: (NSString *)placeStringID
NSString *title = [NSString stringWithFormat:@"title %@", placeStringID];
CLLocationDegrees latitude = [[latDict objectForKey:placeStringID] doubleValue];
CLLocationDegrees longitude = [[longDict objectForKey:placeStringID] doubleValue];
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationDistance regionRadius = 10.0;
return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
radius:regionRadius
identifier:title];
初始化区域监视器
- (void) initializeRegionMonitoring:(NSArray*)geofences
if (locationManager == nil)
//[NSException raise:@"Location Manager Not Initialized" format:@"You must initialize location manager first."];
if(![CLLocationManager regionMonitoringAvailable])
//[self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
return;
for(CLRegion *geofence in geofences) //ERROR HAPPENS HERE AFTER 7 ITERATION
[locationManager startMonitoringForRegion:geofence];
NSLog(@"geofence made here:\n\n%@\n\n",geofences);
错误似乎发生在“for(CLRegion *geofence in geofences)”的第 7 次迭代之后(b/c 有 7 个地理围栏),因为我在“locationManager: manage didStartMonitoringForRegion: region”中的 NSLog 没有出现。
【问题讨论】:
...错误出现在哪一行? 哇!抱歉,现在修复了 so [locationManager startMonitoringForRegion:geofence]; 见下文。这是我愚蠢的错误。糟糕的内存管理,与核心位置无关 【参考方案1】:这个问题与地理围栏、位置或核心位置完全无关。我只是忘记释放我发起的这些可变数组:
NSMutableDictionary *latDict = [[NSMutableDictionary alloc] initWithObjects:lats forKeys:placeID];
NSMutableDictionary *longDict = [[NSMutableDictionary alloc] initWithObjects:longs forKeys:placeID];
刚刚在方法结束时发布了这些字典,一切正常!
【讨论】:
嗯?内存泄漏不会导致段错误。过度释放它确实如此。你的意思是不是偶然的相反?以上是关于地理围栏 iOS 崩溃 EXC_BAD_ACCESS的主要内容,如果未能解决你的问题,请参考以下文章