开发者

Objective-C: EXC_BAD_ACCESS when adding annotation to map

I have the following Objective-C co开发者_如何学运维de:

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

As you can see, I'm getting some data from my website using ASIHttpRequest, parsing the result and hoping to put annotations on the MKMapView.

Trouble is, when I call [self.mapView addAnnotation:...] I keep getting one of these EXC_BAD_ACCESS errors that I simply cannot get to the bottom of.

Anyone have any suggestions?

Many thanks in advance,


By the time the completion block runs self may not be valid. In such a case you need to copy the block to the heap.

You can wrap your block:

[ <your block> copy];

Then there is the issue of releasing the block, many times an auto release works well:

[[ <your block> copy] autorelease];

Other times you may need to explicitly release it.

You might want to typedef your block to make this clearer.


you get BAD_ACCESS because either the mapView or the annotation is already destroyed but you have a pointer to that instances. I guess that you dont retain the mapView. You can check these kind of errors by turning on the NSZombies and by enabling stop on exception.

Or put this code before the line of code where the error happens:

NSLog(@"mapView-desc: %@",[self.mapView description]);
NSLog(@"annotation-desc: %@",[annotation description]);

The BAD_ACCESS should now happen in one of these two lines and then you know on which obejct you have forgotten to retain ;) If there is all fine then the problem is inside mapView or the data in your annotations. The easiest way is to enable NSZombies and wait for messages in your console.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜