ARC and dataWithBytesNoCopy

unsigned char table1DataBytes[] = { 0x01,0x01,0x01,0x01,0x01 };
NSData *table1Data = [NSData dataWithBytesNoCopy: table1DataBytes length: sizeof(table1DataBytes)];

 The code upper will get crashed with error:

malloc: *** error for object 0x2fda57dc: pointer being freed was not allocated

Explaination:

 

As someone on the Apple Developer forums also pointed out, you can also add in the freeWhenDone: NO parameter to dataWithBytesNoCopy and NSData will not free the object. Didn’t even see that there in the reference good catch and the preference over copying the data!

猜你喜欢

转载自shappy1978.iteye.com/blog/1901481
arc