problems with NSMutabledata byteswithnocopy [duplicate]

心已入冬 提交于 2019-12-23 18:28:15

问题


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.

Is there any explanation for these?

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));

回答1:


From Binary Data Programming Guide

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

It gives an error in accessing the buffer after NSData is created.(in memset in below code)

buffer is freed immediately.

The malloced buffer pointer and [data bytes] pointer are different.

NSMutableData create copy of bytes.



来源:https://stackoverflow.com/questions/19380363/problems-with-nsmutabledata-byteswithnocopy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!