The static analyzer in Xcode 8 (beta 1) is warning me that NSString
's initWithBytesNoCopy:length:encoding:freeWhenDone:
can't take ownership of memory allocated by new[]
. Is this an error? If not, why is this the case?
If I change my allocation to use calloc
instead of new[]
, the warning goes away.
The documentation says this about the freeWhenDone
parameter:
If YES, the receiver releases the memory with free() when it no longer needs the data; if NO it won’t.
So freeWhenDone
literally calls free()
when done. To work with new[]
, it would have to delete[]
when done. Hence, the warning message is correct. If you don't want to copy the bytes, then you'll need to set freeWhenDone
to NO
, and delete[]
the bytes when no longer needed.