简体   繁体   English

memcpy()导致iOS中的EXC_BAD_ACCESS

[英]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: 这里有三种可能性:

  1. [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.) (例如,它是什么类型?如果它是NSMutableData ,您应该访问其mutableBytes属性。)
  2. [recorder tempBuffer].mdata is not a valid pointer of sufficient size ( data.length ). [recorder tempBuffer].mdata不是足够大小的有效指针( data.length )。
  3. (__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: 如果目标缓冲区实际上是使用mallocuint8_t (或等效)缓冲区分配的缓冲区,则应该:

  1. Check to make sure that the destination buffer is big enough to hold the entire data contents. 检查以确保目标缓冲区足够大以容纳整个data内容。

  2. 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使用。

  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM