views:

67

answers:

2

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!

+1  A: 

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.

mipadi
Sorry, the array that I'm trying to access is DrumObject.possDrumSounds. Not the defaultSounds.
XenElement
+1  A: 

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

quixoto
If it got overwritten by garbage, why am I able to access it from outside the class?
XenElement
Nevermind, I feel stupid now. You were correct, I didn't retain it, and that's why it didn't work. I was under the silly impression that I didn't have to retain a property. Obviously I need to read the memory management section again.
XenElement