Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problems with NSMutabledata byteswithnocopy [duplicate]

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));
like image 656
thar_bun Avatar asked Jan 31 '26 01:01

thar_bun


1 Answers

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.

like image 139
Parag Bafna Avatar answered Feb 01 '26 15:02

Parag Bafna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!