MKMapView addAnnotation与EXC_BAD_ACCESS崩溃

时间:2011-07-01 16:07:20

标签: iphone mkmapview exc-bad-access

您好我看到这个奇怪的问题,我在for循环中填充我自己的自定义注释到mkmapview,运行for循环54次后是第55次,当我在该行上执行mkmapview addannotation时它会抛出EXC_BAD_ACCESS是代码

- (void)viewDidLoad {

[super viewDidLoad];
[mkMapView setMapType:MKMapTypeStandard];
[mkMapView setZoomEnabled:YES];
[mkMapView setShowsUserLocation:YES];
[mkMapView setScrollEnabled:YES];

MKCoordinateRegion region;
region.center.latitude = currentLocation.latitude;
region.center.longitude = currentLocation.longitude;
MKCoordinateSpan span;
span.latitudeDelta = 0.03;
span.longitudeDelta = 0.03;
region.span = span;
[mkMapView setRegion:region animated:TRUE]; 
[mkMapView setDelegate:self];


int i =0;
for(MyObject* myObject in myObjectArray)
{
    NSLog(@"%d", i);
    if(i == 55){
        NSLog(@"%@", myObject);
    }
    i++;
    NSString *maintitle = myObject.name;
    NSString *subtitle = myObject.address;

    CLLocationCoordinate2D newCoordinate = { [myObject.latitude doubleValue], [myObject.longitude doubleValue] };
    Annotation *pinAnnotation = [[Annotation alloc]initWithCoordinate:newCoordinate title:maintitle subtitle:subtitle image:NULL];
    [mkMapView addAnnotation: pinAnnotation];
    [pinAnnotation release];
}
}

 - (void)dealloc {
mkMapView.delegate = nil;
[mkMapView release];
mkMapView = nil;
[myObjectArray release];
myObjectArray = nil;
[super dealloc];
 }

以下是Annotation.h的代码

 @interface Annotation : NSObject <MKAnnotation>{
 CLLocationCoordinate2D    coordinate;
 NSString * title;
 NSString * subtitle;
 UIImage * image;
 }

 @property (nonatomic, readwrite, assign) CLLocationCoordinate2D    coordinate;
 @property (nonatomic, retain) NSString * title;
 @property (nonatomic, retain) NSString * subtitle;
 @property (nonatomic, retain) UIImage * image;

 - (id) initWithCoordinate:(CLLocationCoordinate2D) coordinate title:(NSString*) title subtitle:(NSString*) subtitle image:(UIImage*) image;

@end

以下是Annotation.m

的代码
 - (id) initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString*) title subtitle: (NSString*) subtitle image:(UIImage*) image{
self = [super init];
if(self){
self.coordinate = coordinate;
self.title = title;
self.subtitle = subtitle;
self.image = image;
}

return self;
}

-(void) dealloc{
[title release];
[subtitle release];
[image release];
[super dealloc];
}

这里是stacktrace

0 MYAPP 0x00099c6a + [TFCrashHandler backtrace] + 46

1 MYAPP 0x0009a007 TFSignalHandler + 45

2 libSystem.B.dylib 0x9455345b _sigtramp + 43

3 ??? 0xffffffff 0x0 + 4294967295

4 MapKit 0x015f5124 - [MKQuadTrie包含:] + 40

5 MapKit 0x0156363c - [MKAnnotationContainerView addAnnotation:] + 356

6 MapKit 0x01538807 - [MKMapView addAnnotation:] + 115

1 个答案:

答案 0 :(得分:12)

确保您尝试添加的所有注释都具有理智的坐标,因为这是将其添加到MKMapView时常见的崩溃源。

纬度应在-90到90之间,经度应在-180到180之间。