下,我有一些代码,似乎是安全的,因为我可以把它...但我越来越EXC_BAD_ACCESS
。不经常介意,只是这一次。的iOS stringWithUTF8String(真的CFAllocatorAllocate)EXC_BAD_ACCESS崩溃,ARC
的代码:
- (void) deviceListenerCallback:(DeviceEventTypeDef)type data:(void*)data
{
DeviceInfoDef* o = (DeviceInfoDef*)data;
char newName[256];
if (o->DeviceName) {
strncpy(newName, o->DeviceName, 255);
} else {
strncpy(newName, "(null)", 255);
}
NSString *deviceNameUTF8 = [NSString stringWithUTF8String:(const char *)newName];
'数据' 是由一个C库传递给我们的指针。数据是存在的,并且没有损坏。
从strncpy()函数结束在了newName的字符为 “网络音频\ 0 \ 0 \ 0 \ 0 \ 0 ...”
的EXC_BAD_ACCESS
是当我打电话stringWithUTF8String的最后一行。错误是:
EXC_BAD_ACCESS (code=1, address=0x30303030)
调用堆栈是:
CFAllocatorAllocate
................................
5 +[NSString stringWithUTF8String:]
6 -[MyClass deviceListenerCall...
该项目ARC下运行。
任何人都可以指出新手的事情我必须做错了什么?
感谢,
-Ken
的原因strncpy()函数是O可能会为NULL。在调用代码有没有保证,它不是,那么断言是不会来这里是正确的做法。 – 2013-03-27 11:48:15
@KenCorey使用'函数strncpy()'不会在'O'是'NULL'两种情况下提供帮助。因此,正确的做法是将'NSAssert'改为'if(o!= NULL)'测试。如果'o'可以是'NULL',那么提到这可能有助于回答你的问题。 – trojanfoe 2013-03-27 11:50:16