[英]memcpy() leading to EXC_BAD_ACCESS in iOS
I am getting NSData
on my socket receiving function and I'm trying to copy that data in a tempbuffer
of my audio class, I am using external type global variable to do so. 我在我的套接字接收函数上获取
NSData
,并且我正在尝试将该数据复制到我的音频类的tempbuffer
中,我正在使用外部类型全局变量来执行此操作。
This is my code: 这是我的代码:
memcpy([recorder tempBuffer].mdata,(__bridger const void *)data,data.length);
Here recorder is my extern
type global variable of audio class. 这里记录器是我的音频类的
extern
类型全局变量。
When control reaches this line of code an exception is thrown, what possibly be the mistake. 当控制到达这行代码时抛出异常,可能是错误。
There are really three possibilities here: 这里有三种可能性:
[recorder tempBuffer].mdata
is not a valid pointer. [recorder tempBuffer].mdata
不是有效指针。 (What type is it, for instance? If it's a NSMutableData , you should be accessing its mutableBytes
property.) mutableBytes
属性。) [recorder tempBuffer].mdata
is not a valid pointer of sufficient size ( data.length
). [recorder tempBuffer].mdata
不是足够大小的有效指针( data.length
)。 (__bridger const void *)data
is not a valid pointer of sufficient size. (__bridger const void *)data
不是足够大小的有效指针。 Of the three, I can guarantee that #3 needs addressing. 在这三个中,我可以保证#3需要解决。 A NSData is not itself the data you want, but an object wrapping the data you want.
NSData本身不是您想要的数据,而是包装所需数据的对象。 Instead of using a bridge here, you should be using
data.bytes
. 您应该使用
data.bytes
,而不是在这里使用data.bytes
。
The other two, I can't help you with. 另外两个,我帮不了你。 I don't know what type
mdata
is or where it was allocated. 我不知道
mdata
类型或分配的位置。
If the destination buffer is really a buffer you allocated with malloc
or a uint8_t
(or equivalent) buffer, you should: 如果目标缓冲区实际上是使用
malloc
或uint8_t
(或等效)缓冲区分配的缓冲区,则应该:
Check to make sure that the destination buffer is big enough to hold the entire data
contents. 检查以确保目标缓冲区足够大以容纳整个
data
内容。
Don't try to cast the NSData
to a (void *)
pointer, but rather use: 不要尝试将
NSData
转换为(void *)
指针,而是使用:
memcpy(destination, data.bytes, data.length);
If the NSData
is not in a contiguous block (which in iOS 7 and later, it might not be), data.bytes
will copy it to a contiguous buffer, which you can then use with memcpy
. 如果
NSData
不在连续的块中(在iOS 7及更高版本中,它可能不是), data.bytes
会将其复制到连续的缓冲区,然后您可以将其与memcpy
使用。
Or better, you can avoid this redundant copy, by removing memcpy
altogether: 或者更好的是,您可以通过完全删除
memcpy
来避免此冗余副本:
[data getBytes:destination length:data.length];
This will, if the NSData
is not in a contiguous block, avoid having data.bytes
copy it to a contiguous buffer which you would then copy again with the memcpy
. 如果
NSData
不在连续的块中,则避免将data.bytes
复制到连续的缓冲区,然后再使用memcpy
复制。
Bottom line, NSData
has a rich interface that should eliminate the need to use low-level memcpy
calls yourself. 最后,
NSData
有一个丰富的界面,不需要自己使用低级memcpy
调用。
From the question, it's not clear what [recorder tempBuffer].mdata
is and how you allocated it, so perhaps you can clarify. 从这个问题来看,目前尚不清楚
[recorder tempBuffer].mdata
是什么以及你如何分配它,所以也许你可以澄清一下。 Hopefully that's not another NSData
object that you're trying to copy into. 希望这不是您要复制到的另一个
NSData
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.