MKMapView addAnnotation 因 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];
}
这是堆栈跟踪
0 MYAPP 0x00099c6a +[TFCrashHandler 回溯] + 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
Hello I am seeing this weird issue, i am populating my own custom annotation to mkmapview in a for loop, after running the for loop for 54 times that is on 55th times when i do mkmapview addannotation on that line it throws EXC_BAD_ACCESS here are code
- (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];
}
Here is the code for 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
Here is the code for 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];
}
here are the 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 contains:] + 40
5 MapKit 0x0156363c -[MKAnnotationContainerView addAnnotation:] + 356
6 MapKit 0x01538807 -[MKMapView addAnnotation:] + 115
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您尝试添加的所有注释都具有合理的坐标,因为这是将它们添加到 MKMapView 时发生崩溃的常见原因。
纬度应介于 -90 到 90 之间,经度应介于 -180 到 180 之间。
Make sure that all annotations you're trying to add has sane coordinates, as this is a common source of crashes when adding them to an MKMapView.
Latitudes should be between -90 and 90 and longitudes should be between -180 and 180.