首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Objective-C:向地图添加注记时的EXC_BAD_ACCESS

Objective-C:向地图添加注记时的EXC_BAD_ACCESS
EN

Stack Overflow用户
提问于 2011-09-25 15:59:18
回答 2查看 1.3K关注 0票数 2

我有以下Objective-C代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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错误,我根本无法找到它的底端。

有人有什么建议吗?

在此之前,非常感谢,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-25 16:36:38

获取BAD_ACCESS是因为mapView或注释已经被销毁,但是您有一个指向这些实例的指针。我猜你不会保留mapView吧。你可以通过turning on the NSZombiesenabling stop on exception来检查这类错误。

或者将此代码放在发生错误的代码行之前:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
NSLog(@"mapView-desc: %@",[self.mapView description]);
NSLog(@"annotation-desc: %@",[annotation description]);

BAD_ACCESS现在应该出现在这两行中的某一行,然后您就知道忘记保留哪一行了;)如果一切正常,那么问题出在mapView内部或注释中的数据上。最简单的方法是启用NSZombies并在控制台中等待消息。

票数 1
EN

Stack Overflow用户

发布于 2011-09-25 16:39:47

在完成block运行时,self可能无效。在这种情况下,您需要将block复制到heap

你可以包装你的块:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[ <your block> copy];

然后是释放块的问题,很多时候自动释放效果很好:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[[ <your block> copy] autorelease];

其他时候,您可能需要显式地释放它。

您可能想要typedef您的块,以使这一点更清楚。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7546681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文