我有以下Objective-C代码:
NSString *urlStr=[[NSString alloc] initWithFormat:@"http://www.prestocab.com/driver/ajax/getFriendsOnMap.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setDelegate:self];
[request setPostValue:[NSString stringWithFormat:@"%f",swCoord.latitude ] forKey:@"sw_lat"];
[request setPostValue:[NSString stringWithFormat:@"%f",swCoord.longitude ] forKey:@"sw_lng"];
[request setPostValue:[NSString stringWithFormat:@"%f",neCoord.latitude ] forKey:@"ne_lat"];
[request setPostValue:[NSString stringWithFormat:@"%f",neCoord.longitude ] forKey:@"ne_lng"];
[request setCompletionBlock:^{
NSLog(@"%@",[request responseString]);
SBJsonParser *parser=[[SBJsonParser alloc]init];
//NSDictionary *obj=[parser objectWithString:[request responseString] error:nil];
NSDictionary *arr=[parser objectWithString:[request responseString] error:nil];
MapViewAnnotation *annotation=[[MapViewAnnotation alloc]init];
for(int i=0;i<arr.count;i++){
NSDictionary *obj=[arr objectForKey:[NSString stringWithFormat:@"%d",i]];
CLLocationCoordinate2D coord;
coord.latitude=[[obj objectForKey:@"lat"] doubleValue];
coord.longitude=[[obj objectForKey:@"lng"] doubleValue];
[annotation initWithTitle:[obj objectForKey:@"uname"] andCoordinate:coord];
//[self.mapView performSelectorOnMainThread:@selector(addAnnotation) withObject:annotation waitUntilDone:YES];
[self.mapView addAnnotation:annotation];
}
[annotation release];
//[self.mapView addAnnotations:annotations];
//[annotations release];
}];
[request setFailedBlock:^{
}];
[request startAsynchronous];
如你所见,我正在使用ASIHttpRequest从我的网站获取一些数据,解析结果,并希望将注释放到MKMapView上。
问题是,当我调用self.mapView addAnnotation:...我不断地收到这样的EXC_BAD_ACCESS错误,我根本无法找到它的底端。
有人有什么建议吗?
在此之前,非常感谢,
发布于 2011-09-25 16:36:38
获取BAD_ACCESS是因为mapView或注释已经被销毁,但是您有一个指向这些实例的指针。我猜你不会保留mapView吧。你可以通过turning on the NSZombies和enabling stop on exception来检查这类错误。
或者将此代码放在发生错误的代码行之前:
NSLog(@"mapView-desc: %@",[self.mapView description]);
NSLog(@"annotation-desc: %@",[annotation description]);
BAD_ACCESS现在应该出现在这两行中的某一行,然后您就知道忘记保留哪一行了;)如果一切正常,那么问题出在mapView内部或注释中的数据上。最简单的方法是启用NSZombies
并在控制台中等待消息。
发布于 2011-09-25 16:39:47
在完成block
运行时,self
可能无效。在这种情况下,您需要将block
复制到heap
。
你可以包装你的块:
[ <your block> copy];
然后是释放块的问题,很多时候自动释放效果很好:
[[ <your block> copy] autorelease];
其他时候,您可能需要显式地释放它。
您可能想要typedef
您的块,以使这一点更清楚。
https://stackoverflow.com/questions/7546681
复制相似问题