views:

36

answers:

1

I have some pins that the user can add. In the callout there is a button. When the user presses the button, I want a new pin to drop in that location, and I want to get rid of the pin that the user pressed (different types of pins).

Basically the first pin is draggable, and when the user has found a proper location they will "lock" it. (the locked pin acts differently in a few ways, which is why I need to replace the old pin)

anyways, this is the method where I do my processing. When I get to the [mapView removeAnnotation:view.annotation]; part all I get is "Program received signal: “EXC_BAD_ACCESS”."

Can anybody help me out here? (The issue is not that the new annotation does not appear, as it does appear. The issue is that the old annotation does not dissapear). EDIT: Fixed code as per suggestion.

    - (void) mapView:(MKMapView *)MapView   
      annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control {
    LockedPotholeAnnotation *annotation = [[[LockedPotholeAnnotation alloc] initWithCoordinate:view.annotation.coordinate addressDictionary:nil]autorelease];
     NSString *titleString = [NSString stringWithFormat:@"Pothole at %.4f, %.4f", view.annotation.coordinate.latitude, view.annotation.coordinate.longitude];
     annotation.title = titleString;
     [mapView addAnnotation:annotation];
     //[annotation release];
     NSLog(@"Added Pin");
     NSLog(@"VA: %@", [view.annotation class]);
     [mapView removeAnnotation:view.annotation];
     //[mapView removeAnnotations:mapView.annotations];
     [mapView setNeedsDisplay]; 
}
+1  A: 

This may not be the only thing, but the first thing that leaps out is that you autorelease the annotation on the line where you alloc it. Therefore you shouldn't also release it after you've added it to mapView.

As this stands, the annotation will likely be prematurely deallocated when the autorelease pool drains -- and if not exactly then, then at some subsequent point that is still premature. The map view will be stuck with a stale pointer and boom.

Not sure why that would manifest quite as soon as you describe, so there may be something else too...

walkytalky
Ahh I can see the mistake. Aside from that, I dont seem to have a problem with the new annotation. the new "locked" annotation appears, yet the old one does not disappear, and this is in fact the issue.
DJ SymBiotiX
@DJ In that case it's hard to say -- the code doesn't seem obviously wrong, so the problem could be elsewhere. Check that the passed in `view` is indeed the right one and that `view.annotation` is in the map view's `annotations` array before you try removing. Does removal work if you do it *before* adding the new annotation? If you don't add a new one at all? What happens when you remove everything, as in the commented out line? Etc - sorry I can't suggest anything more helpful than basic debugging drudgery...
walkytalky
Turns out that the original place where I added annotations, I also had that extra release in there. Which screwed it up when I tried to remove said annotation. Removing that line fixed it. Thanks a lot.
DJ SymBiotiX