Objective-C 中的 EXC_BAD_ACCESS 错误
我时不时地会遇到一个 EXC_BAD_ACCESS
错误,这些错误似乎困扰着新的 Objective-C 程序员。
在我的程序中,我试图获取以秒为单位的时间,将其转换为字符串,然后将其转换为 NSData 对象以写入文件。这是我正在使用的代码,但每次运行它时都会崩溃并显示 EXC_BAD_ACCESS
。我做错了什么?
-(void) startTheClock{
NSTimeInterval cloqInTime = [NSDate timeIntervalSinceReferenceDate];
NSString * dateStr = [self stringFromTimeInterval:cloqInTime];
NSData * data = [[dateStr stringByAppendingString:@", "] dataUsingEncoding:NSUTF8StringEncoding];
NSLog([@"Data:" stringByAppendingString:[data description]]);
// [data retain]; // <-- Uncommenting this and the [data release] line doesn't prevent the error
[self writeData:data]; // <-- EXC_BAD_ACCESS happens here!
// [data release];
}
当我运行此方法时,我得到以下输出:
timeString: |2011-11-04 16:17:12|
Data:<32303131 2d31312d 30342031 363a3137 3a31322c 20>
按照要求,这里是我的 stringFromTimeInterval
方法:
-(NSString *) stringFromTimeInterval:(NSTimeInterval)t{
NSDate * date = [NSDate dateWithTimeIntervalSinceReferenceDate:t];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *timeString = [dateFormat stringFromDate:date];
NSLog(@"timeString: %@", timeString);
return timeString;
}
这里是堆栈跟踪:
#0 0x00007fff84407e90 in objc_msgSend ()
#1 0x00000001054543f0 in 0x00000001054543f0 ()
#2 0x00000001000012ee in -[WorqAppDelegate startTheClock] at /Users/slayton/Documents/Xcode/Worq/Worq/WorqAppDelegate.m:61
#3 0x0000000100001164 in -[WorqAppDelegate cloqInAction:] at /Users/slayton/Documents/Xcode/Worq/Worq/WorqAppDelegate.m:37
#4 0x00007fff8cbb9a1d in -[NSObject performSelector:withObject:] ()
#5 0x00007fff86f69710 in -[NSApplication sendAction:to:from:] ()
#6 0x00007fff86f69642 in -[NSControl sendAction:to:] ()
#7 0x00007fff86f6956d in -[NSCell _sendActionFrom:] ()
#8 0x00007fff86f68a30 in -[NSCell trackMouse:inRect:ofView:untilMouseUp:] ()
#9 0x00007fff86fe88e0 in -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] ()
#10 0x00007fff86f6763a in -[NSControl mouseDown:] ()
#11 0x00007fff86f320e0 in -[NSWindow sendEvent:] ()
#12 0x00007fff86eca68f in -[NSApplication sendEvent:] ()
#13 0x00007fff86e60682 in -[NSApplication run] ()
#14 0x00007fff870df80c in NSApplicationMain ()
#15 0x0000000100001092 in main ()
这里是 writeData
方法,recordFile 是
NSFileHandle
的一个实例:
-(BOOL) writeData:(NSData *)data{
if (recordFile != NULL)
[recordFile writeData:data];
else
NSLog(@"Record file is null! No data written");
}
Every now and then I get one of the EXC_BAD_ACCESS
errors that seem to plague new Objective-C programmers.
In my program I'm trying to get a get the time in seconds, convert that to a string and then convert that to a NSData
object for writing to a file. Here is the code I'm using, but it crashes with an EXC_BAD_ACCESS
every time I run it. What am I doing wrong?
-(void) startTheClock{
NSTimeInterval cloqInTime = [NSDate timeIntervalSinceReferenceDate];
NSString * dateStr = [self stringFromTimeInterval:cloqInTime];
NSData * data = [[dateStr stringByAppendingString:@", "] dataUsingEncoding:NSUTF8StringEncoding];
NSLog([@"Data:" stringByAppendingString:[data description]]);
// [data retain]; // <-- Uncommenting this and the [data release] line doesn't prevent the error
[self writeData:data]; // <-- EXC_BAD_ACCESS happens here!
// [data release];
}
When I run this method I get the following output:
timeString: |2011-11-04 16:17:12|
Data:<32303131 2d31312d 30342031 363a3137 3a31322c 20>
As requested here is my stringFromTimeInterval
method:
-(NSString *) stringFromTimeInterval:(NSTimeInterval)t{
NSDate * date = [NSDate dateWithTimeIntervalSinceReferenceDate:t];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *timeString = [dateFormat stringFromDate:date];
NSLog(@"timeString: %@", timeString);
return timeString;
}
Here is the stacktrace:
#0 0x00007fff84407e90 in objc_msgSend ()
#1 0x00000001054543f0 in 0x00000001054543f0 ()
#2 0x00000001000012ee in -[WorqAppDelegate startTheClock] at /Users/slayton/Documents/Xcode/Worq/Worq/WorqAppDelegate.m:61
#3 0x0000000100001164 in -[WorqAppDelegate cloqInAction:] at /Users/slayton/Documents/Xcode/Worq/Worq/WorqAppDelegate.m:37
#4 0x00007fff8cbb9a1d in -[NSObject performSelector:withObject:] ()
#5 0x00007fff86f69710 in -[NSApplication sendAction:to:from:] ()
#6 0x00007fff86f69642 in -[NSControl sendAction:to:] ()
#7 0x00007fff86f6956d in -[NSCell _sendActionFrom:] ()
#8 0x00007fff86f68a30 in -[NSCell trackMouse:inRect:ofView:untilMouseUp:] ()
#9 0x00007fff86fe88e0 in -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] ()
#10 0x00007fff86f6763a in -[NSControl mouseDown:] ()
#11 0x00007fff86f320e0 in -[NSWindow sendEvent:] ()
#12 0x00007fff86eca68f in -[NSApplication sendEvent:] ()
#13 0x00007fff86e60682 in -[NSApplication run] ()
#14 0x00007fff870df80c in NSApplicationMain ()
#15 0x0000000100001092 in main ()
Here is the writeData
method, recordFile
is an instance of NSFileHandle
:
-(BOOL) writeData:(NSData *)data{
if (recordFile != NULL)
[recordFile writeData:data];
else
NSLog(@"Record file is null! No data written");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
密切关注您的
recordFile
对象。我想知道这个对象是否已被过度释放(并因此被释放),但不是nil
。这将导致[recordFile writeData:data]
方法抛出 EXC_BAD_ACCESS。recordFile
的生命周期是多少?Keep an eye on your
recordFile
object. I'm wondering if this object has been over-released (and thus deallocated) but is notnil
. This would cause the[recordFile writeData:data]
method to throw an EXC_BAD_ACCESS. What is the lifespan ofrecordFile
?冒着风险,但我想说
data
可能是nil
。您检查过data
的值吗?Going out on a limb, but I'd say that
data
is probablynil
. Have you checked the value ofdata
?调试器,特别是堆栈跟踪是你的朋友,但是,是的,最有可能的数据是零。
在调试器窗口中的提示符下,键入: po data
或更好,只需查看左侧窗格中的局部变量并查看数据的值是什么。
the debugger and in particular the stack trace is your friend, but yeah, most likely data is nil.
in the debugger window at the prompt, type: po data
or better, just look at the local variables in the left pane and see what data's value is.