I have a C API I call into as follows:
- (NSData*)getFileContents {
NSData *fileContents;
//do something
return fileContents;
}
-(void*) manipulateFile {
UInt8 *data = (UInt8*) [[self getFileContents] bytes];
UInt32 dataLength = (UInt32)[[self getFileContents] length];
//call some C API with the data and dataLength value
}
Now my question as I don't have to explicitly call free(data)
I am wondering what is happening under the hood of bytes
call
Ask yourself this - did you explicitly call malloc
and friends? Does the documentation for NSData bytes
state that ownership of the pointer is passed to the caller?
Since the answer is no to both questions, then no, you do not (and must not) call free
.
Your bigger concern is the valid lifetime of the pointer. Make no attempt to use it beyond the lifetime of the NSData
object.