objective c – Can’t seem to remove any annotations – Education Career Blog

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:nilautorelease;
     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; 
}

,

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…

Leave a Comment