J'écris une application iPhone où l'utilisateur est censé pouvoir allumer et éteindre toutes les broches sur la carte. Pour mettre toutes les broches sur la carte J'utilise:EXC_BAD_ACCESS en supprimant MKAnnotations
-(void) putAllPins {
for (id key in myDictionary) { //A NSDictionary
NSArray *data = [myDictionary objectForKey:key];
[self putPin: data];
}
isShowingAllPins = TRUE;
}
-(CLLocationCoordinate2D) putPin:(NSArray *) data {
NSNumber *lat = [data objectAtIndex:0];
NSNumber *lon = [data objectAtIndex:1];
NSString *name = [data objectAtIndex:2];
NSString *info = [data objectAtIndex:3];
CLLocationCoordinate2D coords = {[lat doubleValue], [lon doubleValue]};
MyMapAnnotation *annotation = [[MyMapAnnotation alloc] initWithCoordinate:coords andName:name andInformation:info];
[_mapView addAnnotation:annotation];
[annotation release];
return coords;
}
Pour les supprimer, je l'utilise:
-(void) removeAllPins {
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:([_mapView.annotations count] - 1)];
for (id annotation in _mapView.annotations) {
if (annotation != _mapView.userLocation) {
[toRemove addObject:annotation];
}
}
[_mapView removeAnnotations:toRemove];
}
Il fonctionne très bien pour enlever toutes les broches une fois et les ajouter à nouveau. Mais dès que je les supprime une deuxième fois, j'obtiens une erreur EXC_BAD_ACCESS. J'ai tracé le problème à la méthode dealloc dans ma classe d'annotation, mais je ne sais toujours pas quoi faire. Toute aide est appréciée!
MyAnnotation.m:
@implementation MyAnnotation
@synthesize coordinate = _coordinate;
-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate andName:(NSString *)name andInformation:(NSString *)info {
self = [super init];
_coordinate = coordinate;
_name = name;
_info = info;
return self;
}
-(NSString *)title {
return [NSString stringWithFormat:@"PREFIX %@", _name];
}
-(NSString *)subtitle {
return _info;
}
- (void)dealloc {
[_name release];
[_info release];
[super dealloc];
}
@end
Hmm, _name et _info ne sont jamais conservés et cela devrait signifier que la méthode dealloc n'est pas supposée être là du tout, je suppose? Après l'enlèvement de celui-ci, tout fonctionne bien. Merci de le signaler! –
Pas exactement. Qu'est-ce qui fait en sorte que ces chaînes ne soient pas désaffectées dans votre classe? La chose normale est de les conserver si vous voulez leur envoyer des messages plus tard et les libérer dans dealloc. Vous avez juste besoin de vous assurer d'équilibrer conserver/libérer. –