重用从文件读取的代码仅在第二类中导致 EXC_BAD_ACCESS
我在两个不同的文件中使用此代码,从文本文件中的文本字符串填充 categories
数组,其中条目由双管道分隔。
在第一个文件,我的 appViewController
类中,一切都很好。在第二个 popoverViewController
中,程序在 arrayWithArray:
行上使用 EXC_BAD_ACCESS 进行轰炸。两个接口文件中的 categories
、tempArray
、diskfile
和 textFromFile
声明是相同的。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置 NSZombieEnabled,MallocStackLogging 和 在调试器中保护 malloc。然后,当您的应用程序崩溃时,在 gdb 控制台中输入以下内容:
将 0x543216 替换为导致崩溃的对象的地址,您将获得更有用的堆栈跟踪,它应该可以帮助您查明位置代码中导致问题的确切行。
请参阅本文以获取更详细的说明。
Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the gdb console:
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.