[bytes] 会自动释放内存而不必使用 free 吗?

Does [bytes] automatically release the memory without having to use free?

我有一个CAPI我调入如下:

- (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          

}

现在我的问题是我不必显式调用 free(data) 我想知道 bytes 调用

的幕后发生了什么

问问自己 - 你是否明确地打电话给 malloc 和朋友? NSData bytes 的文档是否声明指针的所有权已传递给调用者?

既然两个问题的答案都是否定的,那么不,你不会(也不能)打电话给free

您更关心的是指针的有效生命周期。不要尝试在 NSData 对象的生命周期之后使用它。