NSMutabledata byteswithnocopy 的问题 [英] problems with NSMutabledata byteswithnocopy

查看:69
本文介绍了NSMutabledata byteswithnocopy 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个代码来从 malloced 缓冲区创建 NSMutableData.我使用了 freewhendone = YES
1) 创建 NSData 后访问缓冲区时出错.(在下面代码的 memset 中)
2)malloced的缓冲区指针和[数据字节]指针不同.

I am trying to write a code to create NSMutableData from malloced buffer.I used freewhendone = YES
1) It gives an error in accessing the buffer after NSData is created.(in memset in below code)
2) The malloced buffer pointer and [data bytes] pointer are different.

对这些有什么解释吗?

UWORD8 *rgb_buffer = malloc(u4_stride * u4_height * 3);

NSMutableData *rgbData = [NSMutableData dataWithBytesNoCopy:rgb_buffer

                                                     length:(u4_stride * u4_height * 3)

                                               freeWhenDone:YES];
memset(rgb_buffer, 0, (u4_stride * u4_height * 3));

推荐答案

来自 二进制数据编程指南

NSMutableData 响应 dataWithBytesNoCopy:length:,但是字节被复制无论如何,缓冲区会立即释放.

NSMutableData responds to dataWithBytesNoCopy:length:, but the bytes are copied anyway and the buffer is freed immediately.

在创建 NSData 后访问缓冲区时出错.(在下面代码的 memset 中)

立即释放缓冲区.

malloced 缓冲区指针和 [data bytes] 指针不同.

NSMutableData 创建字节副本.

这篇关于NSMutabledata byteswithnocopy 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆