Objective C --- 数组在 (void) 函数中可读,但在 (IBAction) 中不可读
大家好,我有一个数组,其中包含我的应用程序中的所有 .aif 文件路径,这是我通过使用 mainBundle 和资源找到的。
该数组需要通过 2 个视图控制器向下传递才能到达实际使用的地方。问题是,它要么崩溃,要么记录完全错误的内容。当我调试时,我在崩溃时收到 EXC_BAD_ACCESS 注释,但当我正常运行它时,它只是崩溃了。
这是它起作用的地方;
- (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);
}
这将返回一长串以 fileName.aif 结尾的路径。你明白了。
然而......
// 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];
}
该片段导致崩溃。如果我注释掉处理 possDrumSounds 的部分,它就可以正常工作。否则它会崩溃或以某种方式更改数组以包含随机对象,例如 UIViewControllers,我不知道它们来自哪里。
感谢所有帮助,谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将在
buildDrumTrigger
中释放defaultSounds
,因此当其他方法尝试访问它时,它会指向已释放的数据。EXC_BAD_ACCESS 表示您正在尝试访问无法访问的内存,通常是因为您已经释放了它。
You're releasing
defaultSounds
inbuildDrumTrigger
, 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.
您可能将数组保留在 DrumObject 内部而不保留它,因此它最终会被垃圾覆盖。
You're probably keeping around array inside of DrumObject without retaining it, so it ends up getting overwritten with garbage.