2012-04-10 6 views
3

Je développe une application iPhone/iPod. Le code suivant est le fichier .m d'un UIViewController. Je reçois le texte suivant:iOS - EXC_BAD_ACCESS Erreur

Thread 1: EXC_BAD_ACCESS (code=2...... 

quand je frappe la ligne suivante:

cell.textLabel.text = [datasource objectAtIndex:indexPath.row]; 

Je comprends que cela se produit généralement lorsque vous essayez d'accéder à un objet après avoir libéré, mais je ne divulguons pas avant d'essayer d'y accéder. J'ai joint le code complet ci-dessous.

Toute aide est reconnaissante!

#import "HomePage.h" 
#import "HusbandryRecordsMain.h" 
#import "TaskManagerMain.h" 
#import "AnimalInventoryMain.h" 
#import "FeedInventoryMain.h" 

@implementation HomePage 
@synthesize options, datasource; 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad { 
    [self setupArray]; 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 
} 

-(void)setupArray{ 
    options = [NSMutableArray arrayWithObjects:@"Husbandry Records", @"Task Manager", @"Feeder Inventory", @"Animal Inventory", nil]; 

    datasource = options; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    //#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return 4; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];  
    } 

    [[cell textLabel] setBackgroundColor:[UIColor clearColor]]; 
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]]; 

    // THE FOLLOWING LINE IS THROWING THE ERROR! 
    cell.textLabel.text = [datasource objectAtIndex:indexPath.row]; 

    //Arrow 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (indexPath.row == 0){ 
     HusbandryRecordsMain *hrm = [self.storyboard instantiateViewControllerWithIdentifier:@"Husbandry Records - Main"]; 
     [self.navigationController pushViewController:hrm animated:YES]; 
    } 
    else if (indexPath.row == 1){ 
     TaskManagerMain *tmm = [self.storyboard instantiateViewControllerWithIdentifier:@"Task Manager - Main"]; 
     [self.navigationController pushViewController:tmm animated:YES]; 
    } 
    else if (indexPath.row == 2){ 
     FeedInventoryMain *fim = [self.storyboard instantiateViewControllerWithIdentifier:@"Feeder Inventory - Main"]; 
     [self.navigationController pushViewController:fim animated:YES]; 
    } 
    else if (indexPath.row == 3){ 
     AnimalInventoryMain *aim = [self.storyboard instantiateViewControllerWithIdentifier:@"Animal Inventory - Main"]; 
     [self.navigationController pushViewController:aim animated:YES]; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

//----------------------TABLEVIEWCELL HEIGHT ------------------------------------------- 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return 70; 

} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 
+4

Utilisez ARC sauf si vous avez une raison très convaincante de ne pas le faire. – zaph

+1

Si vous apprenez juste à voler, n'utilisez pas d'arc. – NCFUSN

Répondre

0

Quel type de vos options de fichiers et source de données? Également essayer self.options etc.

+0

datasource est un NSArray et les options sont bien NSMutableArray – comead

+0

. Pourquoi avez-vous besoin de deux baies pour conserver vos données? vous les avez probablement conservés (forts ou retenus). n'utilisez que self.option et n'oubliez pas de le libérer dans la méthode dealloc. – NCFUSN

+0

return 4; peut être return [self.option count]; – NCFUSN

5
-(void)setupArray{ 
    options = [NSMutableArray arrayWithObjects:@"Husbandry Records", @"Task Manager", @"Feeder Inventory", @"Animal Inventory", nil]; 

    datasource = options; 
} 

Vous assignez un objet autoreleased à la variable d'instance directement datasource puis tenter de l'utiliser après sa sortie dans la ligne de code qui s'écraser. Si vous avez activé la détection de zombies, il est très probable que vous l'ayez détecté directement. De même, l'analyseur statique (construire et analyser) aurait dû l'attraper.

(À moins, bien sûr, vous avez ARC a permis à quel point quelque chose d'autre se passe ...)

+0

Qu'est-ce que la détection Zombie? Je ne suis pas familier avec cela. L'ARC est désactivé - dois-je le garder? Et, ok, bien comment je réparerais ça? – comead

+1

La détection de zombies est un outil dans l'instrument de profil. – NCFUSN

+2

Vous devez retenir les options; soit en utilisant 'self.datasource = options;' soit 'datasource = [options retain];'. Vous devriez utiliser ARC globalement, s'il s'agit d'un nouveau code. Cependant, vous devriez vraiment aller apprendre les bases de l'Objective-C et de la gestion de la mémoire, ou ce sera le premier de nombreux problèmes/crashers qui causeront des maux de tête (marcher avant de courir et tout ça). – bbum