2012-04-10 12 views
3

iPhone/iPodアプリケーションを開発中です。次のコードは、UIViewControllerの.mファイルです。私は次の取得:iOS - EXC_BAD_ACCESSエラー

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

私はあなたがそれを解放した後、オブジェクトにアクセスしようとすると、これは一般的に起こることを理解し、私はそれを放出しない:私は次の行を打つ

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

私はそれにアクセスしようとする前に。私は以下の完全なコードを添付しました。

ご協力いただきありがとうございます。

#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

非常に魅力的な理由がない限り、ARCを使用してください。 – zaph

+1

あなたが飛行することを学んでいるだけなら、円弧を使わないでください。 – NCFUSN

答えて

0

どのような種類のファイルのオプションとデータソースの?また、self.optionsなどを試してみてください。

+0

datasourceはNSArrayで、オプションはNSMutableArray – comead

+0

です。なぜあなたのデータを保持するために2つの配列が必要ですか?あなたはおそらくそれらを保持しています(強くまたは保持しています)。 self.optionのみを使用し、deallocメソッドでそれを解放することを忘れないでください。 – NCFUSN

+0

return 4;返すことができます[self.option count]; – NCFUSN

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

    datasource = options; 
} 

あなたが直接datasourceインスタンス変数に自動解放オブジェクトを割り当て、それをクラッシュされているコードの行で放出された後にそれを使用しようとしています。ゾンビ検出をオンにした場合、これは直接検出された可能性があります。同様に、スタティック・アナライザー(ビルドと分析)はそれを捕らえたはずです。

(もちろん、あなたがARCは、他の何かが起こっているこの時点で有効になっている、いない限り...)

+0

ゾンビ検出とは何ですか?私はこれに慣れていない。 ARCがオフになっている - 私はそれを維持する必要がありますか?そして、これはどうしたらいいですか? – comead

+1

ゾンビ検出はプロファイル機器のツールです。 – NCFUSN

+2

オプションを 'retain'する必要があります。 'self.datasource = options;'または 'datasource = [options retain];'のどちらかを使用します。新しいコードの場合は、ARC全体を使用する必要があります。しかし、実際にObjective-Cとメモリ管理の基礎を学ぶか、これが頭痛の原因となる多くの問題/クラッシャの最初のものになるでしょう(実行する前に歩いてください)。 – bbum

関連する問題