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

发布于 2024-09-17 12:58:41 字数 1337 浏览 7 评论 0原文

大家好,我有一个数组,其中包含我的应用程序中的所有 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-09-24 12:59:49

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

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

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.

如日中天 2024-09-24 12:59:35

您可能将数组保留在 DrumObject 内部而不保留它,因此它最终会被垃圾覆盖。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文