Daniel G. Wilson
Daniel G. Wilson

Reputation: 15055

Objective C --- Array is readable in a (void) function but not an (IBAction)

Hey all, I have an array that holds all of the .aif file paths in my app, which I found by using mainBundle and resources.

This array needs to be passed down through 2 view controllers to reach where it is actually used. The problem is, it either crashes or logs something totally wrong. When I debug I get an EXC_BAD_ACCESS note at the crash, but not when I run it normally where it just crashes.

Here's the place where it works;

- (void)buildDrumTrigger {
     defaultSounds = [[NSArray alloc] initWithObjects: @"kick_3", @"aif", @"snare_1", @"aif",            @"HiHat_1", @"aif", @"ride_1", @"aif", @"crash_1", @"aif", @"crash_2", @"aif", @"crash_3", @"aif", @"wave_1", @"aif", nil];
     self.DrumObject = [[DrumTrigger alloc] initWithSounds:defaultSounds:5:volumeBox];
     [defaultSounds release];
     NSLog(@"Possible Sounds: %@", DrumObject.possDrumSounds);
}

That returns a long list of paths that end in fileName.aif. You get the idea.

However...

// Change the current view to the options window.
- (IBAction)goToOptionsView {
    NSLog(@"Loading options menu");
    NSLog(@"DrumObject.drumSounds: %@", DrumObject.drumSounds);
    NSLog(@"DrumObject.possDrumSounds: %@", DrumObject.possDrumSounds);
    optionsViewController.soundBox2 = DrumObject.drumSounds;
    optionsViewController.possDrumSounds = DrumObject.possDrumSounds;
    [self presentModalViewController:optionsViewController animated:YES];
}

That snippet causes a crash. If I comment out the parts where it deals with possDrumSounds, it works fine. Otherwise it crashes or somehow changes the array to contain random objects like UIViewControllers that I have no idea where they came from.

All help appreciated, thanks!

Upvotes: 1

Views: 187

Answers (2)

Ben Zotto
Ben Zotto

Reputation: 71008

You're probably keeping around array inside of DrumObject without retaining it, so it ends up getting overwritten with garbage.

Upvotes: 2

mipadi
mipadi

Reputation: 410662

You're releasing defaultSounds in buildDrumTrigger, so by the time other methods try to access it, it points to data that has been deallocated.

EXC_BAD_ACCESS indicates you're trying to access memory you can't access, normally because you've already released it.

Upvotes: 2

Related Questions