Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning in Custom Map Annotations iPhone

I am using a custom map annotation class for map view in iPhone. Whenever I pop my map view from navigation bar stack I usually see some warnings in console.

MapAnnotation was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:

I am not using KVO in my code hence not able to understand why I am receiving these warnings

like image 992
sandy Avatar asked May 03 '11 16:05

sandy


2 Answers

Latitude and Longitude have differing bounds:

  • (-90, 90) for Lat
  • (-180, 180) for Long

Passing a value outside of those bounds will cause the custom class to be deallocated and therefore giving you the error that you're receiving. Make sure that you are passing the correct values for both Latitude and Longitude.

It would be really nice if Apple passed a bounding error for this instead of an early release error. That would've saved me roughly 5 hours worth of time

like image 82
James Mertz Avatar answered Nov 09 '22 10:11

James Mertz


I was getting the same error as you:

An instance 0xa975400 of class xxxxxx was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0xa980eb0> (
<NSKeyValueObservance 0xa980e70: Observer: 0xa94f8a0, Key path: coordinate, Options: <New: NO, Old: NO, Prior: YES> Context: 0x0, Property: 0xa980ef0>

As pointed here it was caused because I was adding a MKAnnotation with an invalid coordinate to a MKMapView.

My solution was to create a function to check the coordinate valid.

Place+MKAnnotation.m

I created a category of my Place class and

#import "Place.h"
#import <MapKit/MapKit.h>

@interface Place (MKAnnotation) <MKAnnotation>
- (BOOL)coordinateIsValid;
...
@end

Place+MKAnnotation.m

@implementation Place (MKAnnotation)

- (BOOL)coordinateIsValid
{
    return CLLocationCoordinate2DIsValid(CLLocationCoordinate2DMake([self.latitude doubleValue],[self.longitude doubleValue]));

}
...
@end

I only add the annotation in my ViewController if the coordinate is valid.

if([p coordinateIsValid]) {
    [self.mapView addAnnotation:p];
} 
like image 44
Sergio del Amo Avatar answered Nov 09 '22 09:11

Sergio del Amo