MKMapView – Removing annotation causes app crash

Removing annotations from my map view in the following way:

 if ([[self.mapView annotations] count] > 0)
    [self.mapView removeAnnotations:[self.mapView annotations]];

causes my application to crash with the following exception:

  • “No such module” when using @testable in Xcode Unit tests
  • Error 'xcodebuild: Returned an unsuccessful exit code' after trying publishing Pod
  • How to know that if the only visible area of a .png is touched in XCode (swift or objective C)
  • PDF editing with iPhone sdk
  • SpriteKit Incorrectly Detecting Multiple Collisions
  • How to set tcp_nodelay in GCDAsyncsocket?
  • *** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MKAnnotationContainerView 0xe87b420> for the key path "title" from <PFAnnotation 0x10851230> because it is not registered as an observer.'

    The annotations are added in the following way:

     CLLocationCoordinate2D pinPosition;
    for (int index = 0; index < [array count]; index++)
        Station *aStation = [array objectAtIndex:index];
        PFAnnotation *stationPin = [[PFAnnotation alloc] init]; //StationPinView
        pinPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);
        stationPin.stationName = [aStation valueForKey:@"stationName"];
        stationPin.stationPosition = pinPosition;
        stationPin.stationLength = [aStation valueForKey:@"platformLength"];
        [self.mapView addAnnotation:stationPin];
        [stationPin release];        

    My PFAnnotation.h is:

    @interface PFAnnotation : NSObject <MKAnnotation>
        NSString *stationName;
        CLLocationCoordinate2D stationPosition;
        NSNumber *stationLength;
    @property (nonatomic, retain) NSString *stationName;
    @property CLLocationCoordinate2D stationPosition;
    @property (nonatomic, retain) NSNumber *stationLength;

    and my PFAnnotation.m is:

    @implementation PFAnnotation
    @synthesize stationName;
    @synthesize stationPosition;
    @synthesize stationLength;
    - (CLLocationCoordinate2D)coordinate;
        return stationPosition; 
    - (NSString *)title
        return stationName;
    - (NSString *)subtitle
        if (stationLength == nil)
            return nil;
            return [NSString stringWithFormat:@"Platform Length: %@ft",stationLength];
    - (void)dealloc {
        [stationName release];
        [stationLength release];
        [super dealloc];

    I have read in some other threads that, setting the annotation properties from background thread is the reason for the above error. But in my case,it is not so because every thing is performed on the main thread. Please advise.

    4 Solutions Collect From Internet About “MKMapView – Removing annotation causes app crash”

    ok..solved it at last!!! I think it was due to the animation provided during addition of annotations. since there were a number of annotations that were added back-to-back with animation and also the annotations were removed just before the animation started, there could have been a reference to the released annotation(this is my guess). Moreover, the removal+addition process was made on each regionDidChangeAnimated call, which could have made a overlap between the removal and addition process. Anyway, how I solved it was that, I provided a timer which will be fired only after 1 second after every regionDidchangeAnimated to make sure that the user has done with dragging. Thus unnecessary addition+removal of annotations was avoided and I was able to avoid the crash. Thanks to all guys here for their time taken to support me, especially Guntis Treulands.

    In your PFAnnotation class, did you declare both title and subtitle properties as they are in the protocol?

    If Your PFAnnotation really has incorrect setters getters for string values:

    from here:


    - (void) setCaption: (NSString*)input
        [caption autorelease];
        caption = [input retain];


    - (NSString*) caption 
        return caption;


    - (void) dealloc
        [caption release];
        [super dealloc];

    also – it’s easier to provide coordinates in this way: (also works on ios 3.1.3)

    stationPin.stationPosition = (CLLocationCoordinate2D) {[[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]}

    than (only from ios 4)

    stationPin.stationPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);

    Please check whether an explicit removal of observer to the property “title” is being done anywhere in the code.