3

私はiPhone/iPodアプリを開発しています。次のコードは、UIViewControllerの.mファイルです。私は次のようになります:

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

次の行に到達したとき:

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

これは通常、オブジェクトを解放した後にオブジェクトにアクセスしようとしたときに発生することを理解していますが、アクセスしようとする前に解放しません。以下に完全なコードを添付しました。

どんな助けもありがたいです!

#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

2 に答える 2

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

    datasource = options;
}

自動解放されたオブジェクトをdatasourceインスタンス変数に直接割り当て、クラッシュしているコード行で解放された後にそれを使用しようとしています。ゾンビ検出をオンにした場合、これを直接キャッチした可能性が非常に高くなります。同様に、静的アナライザー(ビルドおよび分析)がそれをキャッチする必要があります。

(もちろん、ARCが有効になっていて、その時点で何か他のことが起こっている場合を除きます...)

于 2012-04-10T21:55:55.510 に答える
0

どのタイプのファイルオプションとデータソースですか?self.optionsなども試してください。

于 2012-04-10T21:59:53.773 に答える