11

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:

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


@end

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;
    else
        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.

DroidHeaven
  • 2,414
  • 3
  • 25
  • 31

4 Answers4

5

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.

DroidHeaven
  • 2,414
  • 3
  • 25
  • 31
1

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

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html

Valerio
  • 513
  • 2
  • 14
  • I've added the class files in the question. Do they conform to Apple's requirments? (I'am not an expert developer, pardon me). – DroidHeaven Jun 13 '12 at 10:25
  • I don't understand why you're overriding the getter of title/subtitle...you should just declare the properties and synthesize them; later when you create the annotation just set them – Valerio Jun 13 '12 at 10:56
  • Since I am the maintenance guy, I'm not sure why the original developer wrote like this :). Let me try the route you specified. – DroidHeaven Jun 13 '12 at 11:27
  • Do I need to create properties with the same name - 'title', 'subtitle' and 'coordinate' as in MKAnnotation protocol declaration? – DroidHeaven Jun 13 '12 at 12:05
1

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

from here: http://cocoadevcentral.com/d/learn_objectivec/

Setter:

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

Getter:

- (NSString*) caption 
{
    return caption;
}

Release:

- (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]);
Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
  • yeah..but it seems that is not gonna help either :(. – DroidHeaven Jun 13 '12 at 12:06
  • 1
    can you possibly try implementing these mapPin class files? http://stackoverflow.com/questions/8180513/removing-adding-annotations-to-mapview-cause-memory-leaks/8180605#8180605 hopefully that will fix the problem. – Guntis Treulands Jun 13 '12 at 12:14
  • Nopes :(..I've implemented the class files like the specified in the link but the issue still happens.. – DroidHeaven Jun 13 '12 at 12:29
  • Ok, then obviously problem is not in PFAnnotation, because, when I implemented PFAnnotation in my project - it didn't crash when removing annotations. Can you possibly post some code where you use annotations? – Guntis Treulands Jun 13 '12 at 12:35
  • Ok, then the problem is not in PFAnnotation after all. I also implemented PFAnnotation in my project - and it didn't crash. So can you possibly show code where you use map anotations? for example what happens in these functions:? - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation - (void)mapView:(MKMapView *)mp annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(REVClusterAnnotationView *)view – Guntis Treulands Jun 13 '12 at 12:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12487/discussion-between-balu-ramachandran-and-guntis-treulands) – DroidHeaven Jun 13 '12 at 12:44
1

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

iosDev
  • 21
  • 2
  • 1
    and also please try removal & addition of annotation forcefully in the main thread by explicitly calling "performSelectorOnMainthread". – iosDev Jun 13 '12 at 12:21