重用从文件读取的代码仅在第二类中导致 EXC_BAD_ACCESS

发布于 2024-12-17 14:12:55 字数 888 浏览 4 评论 0原文

我在两个不同的文件中使用此代码,从文本文件中的文本字符串填充 categories 数组,其中条目由双管道分隔。

在第一个文件,我的 appViewController 类中,一切都很好。在第二个 popoverViewController 中,程序在 arrayWithArray: 行上使用 EXC_BAD_ACCESS 进行轰炸。两个接口文件中的 categoriestempArraydiskfiletextFromFile 声明是相同的。

NSLog 跟踪器和断点确认变量的值直到最后一个致命行都是相同的。弹出窗口包含一个选择器,因此选择器委托和数据源协议已就位。这是唯一的区别。谁能解释一下可能发生了什么?

categories=[[NSMutableArray alloc] init];

tempArray = [[NSMutableArray alloc] init] ;

NSMutableString *textFromFile=[[NSString alloc] init];  

NSString *filePath = [[NSBundle mainBundle] pathForResource: @"Categories" ofType: @"txt"];

if (filePath) {
  textFromFile = [NSString stringWithContentsOfFile:filePath];

  categories=[NSMutableArray arrayWithArray:[textFromFile componentsSeparatedByString: @"||"]];
}   

I use this code in two different files to fill the categories array from a string of text from a text file, in which entries are separated by double pipes.

In the first file, my appViewController class, everything is fine. In the second, popoverViewController, the program bombs with EXC_BAD_ACCESS on the arrayWithArray: line. Declarations for categories, tempArray, diskfile, and textFromFile are the same in both interface files.

NSLog tracers and breakpoints confirmed values of variables are the same down to that last fatal line. The popover contains a picker, so picker delegate and datasource protocols are in place. That's the only difference. Can anyone explain what might be going on?

categories=[[NSMutableArray alloc] init];

tempArray = [[NSMutableArray alloc] init] ;

NSMutableString *textFromFile=[[NSString alloc] init];  

NSString *filePath = [[NSBundle mainBundle] pathForResource: @"Categories" ofType: @"txt"];

if (filePath) {
  textFromFile = [NSString stringWithContentsOfFile:filePath];

  categories=[NSMutableArray arrayWithArray:[textFromFile componentsSeparatedByString: @"||"]];
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

何以笙箫默 2024-12-24 14:12:55

设置 NSZombieEnabledMallocStackLogging在调试器中保护 malloc。然后,当您的应用程序崩溃时,在 gdb 控制台中输入以下内容:

(gdb) info malloc-history 0x543216

将 0x543216 替换为导致崩溃的对象的地址,您将获得更有用的堆栈跟踪,它应该可以帮助您查明位置代码中导致问题的确切行。

请参阅本文以获取更详细的说明。

Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the gdb console:

(gdb) info malloc-history 0x543216

Replace 0x543216 with the address of the object that caused the crash, and you will get a much more useful stack trace and it should help you pinpoint the exact line in your code that is causing the problem.

See this article for more detailed instructions.

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