在使用扩展CLPlacemark的自定义类填充NSMutableArray之后删除不良访问

我有一个PlaceAnnotation类,我填充到一个NSMutableArray。 在viewDidLoad中,我发起ihatethis

_ihatethis = [[NSMutableArray alloc]init]; 

我使用MKLocalSearchCompletionHandler进行search。 并处理这样的地图项:

 for (MKMapItem *mapItem in [response mapItems]){ PlaceAnnotation *place = [[PlaceAnnotation alloc] init]; [place assignTitle:[[mapItem placemark] name]; [_ihatethis addObject:place]; } [_ihatethis removeObjectAtIndex:2]; /*BAD ACCESS HERE*/ [_tableView reloadData]; 

这是我的PlaceAnnotation.h文件

 @interface PlaceAnnotation : CLPlacemark <MKAnnotation> @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, readonly, copy) NSString *title; @property (nonatomic) NSDictionary* dict; //@property (nonatomic) NSURL *url; @property (nonatomic) NSString *phoneNum; @property (readonly) BOOL selected; -(void)assignTitle:(NSString *)newTitle; -(void)assignSelected:(BOOL) boolVal; 

这是我的PlaceAnnotation.m文件

 #import "PlaceAnnotation.h" @interface PlaceAnnotation () @property (readwrite) NSString *title; @property (readwrite) BOOL selected; @end @implementation PlaceAnnotation -(void) assignTitle:(NSString *)newTitle { if ( ![newTitle isEqualToString:[self title]]){ self.title = newTitle; } } -(void) assignSelected:(BOOL)boolVal{ self.selected = boolVal; } @end @end 

这是我的第一篇文章,我已经阅读了很多回答exc_bad_access的问题,我无法弄清楚。 所以我觉得不知何故地名被忘记和释放了。 所以当我去删除是后来它不见了。 我真的很困惑,很生气。

如果崩溃确实在这一行[_ihatethis removeObjectAtIndex:2]; 这是一个EXC_BAD_ACCESS,那么有两种可能性:

  1. _ihatethis指向一个破坏的数组(不太可能,如果循环经过)。
  2. 数组中的某个对象在其dealloc方法中已经被过度释放或者破坏了内存pipe理。

这可能是因为你试图删除一个大于数组数的索引。 尝试在[_ihatethis removeObjectAtIndex:2];之前输出计数[_ihatethis removeObjectAtIndex:2]; , 喜欢这个:

 ` NSLog(@"count: %d", [_ihatethis count]); [_ihatethis removeObjectAtIndex:2]; /*BAD ACCESS HERE*/ ` 

如果计数小于3,那么您试图删除数组边界外的索引处的对象。

我改变了这一点

 @interface PlaceAnnotation : CLPlacemark <MKAnnotation> 

对此

 @interface PlaceAnnotation : NSObject <MKAnnotation> 

所以我想这个问题是与CLPlacemark的dealloc。 谢谢