当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: