Objective C - 数组在(void)函数中是可读的,但不是(IBAction)

时间:2022-06-01 13:32:44

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.

嘿所有,我有一个数组,包含我的应用程序中的所有.aif文件路径,我通过使用mainBundle和资源找到。

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.

需要通过2个视图控制器向下传递此数组,以达到实际使用的位置。问题是,它要么崩溃,要么记录完全错误的东西。当我调试时,我在崩溃时获得了一个EXC_BAD_ACCESS注释,但是当我正常运行时它不会崩溃。

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.

这将返回以fileName.aif结尾的一长串路径。你明白了。

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.

该片段导致崩溃。如果我注释掉处理possDrumSounds的部分,它可以正常工作。否则它会崩溃或以某种方式更改数组以包含UIViewControllers之类的随机对象,我不知道它们来自何处。

All help appreciated, thanks!

所有帮助表示赞赏,谢谢!

2 个解决方案

#1


2  

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

你可能会保留DrumObject中的数组而不保留它,因此它最终会被垃圾覆盖。

#2


2  

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

您正在buildDrumTrigger中释放defaultSounds,因此当其他方法尝试访问它时,它指向已释放的数据。

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

EXC_BAD_ACCESS表示您正在尝试访问无法访问的内存,通常是因为您已经发布了它。

#1


2  

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

你可能会保留DrumObject中的数组而不保留它,因此它最终会被垃圾覆盖。

#2


2  

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

您正在buildDrumTrigger中释放defaultSounds,因此当其他方法尝试访问它时,它指向已释放的数据。

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

EXC_BAD_ACCESS表示您正在尝试访问无法访问的内存,通常是因为您已经发布了它。