문제

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