Вопрос

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));
Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top