Warning in Custom Map Annotations iPhone

2020-02-01 02:19发布

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

4条回答
我想做一个坏孩纸
2楼-- · 2020-02-01 02:31

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];
} 
查看更多
▲ chillily
3楼-- · 2020-02-01 02:33

Fixed it. I was using a wrong pair of latitude and longitude in annotations, I have changed the same and now everything seems to be perfect and warning has been disappeared as well.

查看更多
萌系小妹纸
4楼-- · 2020-02-01 02:36

Are you autoreleasing your annotation before adding it to the MapView?

If so, try just allocating it, add it to the MapView, then release it.

查看更多
SAY GOODBYE
5楼-- · 2020-02-01 02:51

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

查看更多
登录 后发表回答