Christian Gossain
Christian Gossain

Reputation: 5972

I'm getting a EXC_BAD_ACCESS when using the viewWillAppear:(BOOL)animated method

So I'm accessing some data (settings) from a .plist as my view appears. When I use the viewDidAppear:(BOOL)animated method to access that data everything works just fine and dandy. But when I access the data using the viewWillAppear:(BOOL)animated method to access my data everything stops and I get an EXC_BAD_ACCESS error.

Can someone please help me out with this?

- (void)viewDidAppear:(BOOL)animated {

    NSLog(@"View Will Appear method");

    NSString *filePath = [self settingsFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];

        if ([[array objectAtIndex:0] intValue] == 0) {
            UIImage *imageLow = [UIImage imageNamed:@"image1.png"];
            [object1 setImage:imageLow];
            [imageLow release];
            unitRatio = 1.8;
        }
        else if ([[array objectAtIndex:0] intValue] == 1) {
            UIImage *imageHigh = [UIImage imageNamed:@"image2.png"];
            [object1 setImage:imageHigh];
            [imageHigh release];
            unitRatio = 0.9;
        }

        [array release];

    }
    else {
        UIImage *imageLow = [UIImage imageNamed:@"image1.png"];
        [object1 setImage:imageLow];
        [imageLow release];
        unitRatio = 1.8;
    }

}

Upvotes: 1

Views: 1267

Answers (2)

Count Chocula
Count Chocula

Reputation: 1041

Also, you must have a call to super in your viewWillAppear method:

[super viewWillAppear:animated]

That's not what is causing the crash, but the docs say it's required.

Upvotes: 2

grahamparks
grahamparks

Reputation: 16296

The [imageLow release] and [imageHigh release] calls are unnecessary and will cause a crash. The objects returned by imageNamed: are pre-autoreleased, so you don't need to release them yourself.

Upvotes: 4

Related Questions