Why must I free this data? Am I the owner?
This is strange. In a technical Q & A, Apple says this:
void *data = CGBitmapContextGetData (cgctx);
if (data != NULL)
{
// **** You have a pointer to the image data ****
// **** Do stuff with the data here ****
}
// When finished, release the context
CGContextRelease(cgctx);
// Free image data memory for the context
if (data)
{
free(data);
}
I looked up the documentation for CGBitmapContextGetData, and it does not mention that I am responsible for releasing the data when I call this. There is already the CGContextRelease call which cleans up the context.
What is the point of having to extra-release the data inside the context? It is just a pointer to that data, right? Why do they call free(data)
here?
Update : I neglected to read the entire code example. Apple allocates bitmapData
in CreateARGBBitmapContext()
but does not free it. Which is perfectly fine, since the context still needs to do draw into it.
However, bitmapData
will have to later be released when they're done with the drawing, which is exactly what they do at the end of ManipulateImagePixelData()
. So while it is unusual to release something that was obtained by a get function, it the sample code is correct .
To avoid the confusion of freeing something that was returned from a get function, one might want to store the bitmap data in a global / instance variable and free()
that when done.
I would assume this to be a bug in the code example. The documentation of this function does not mention anything special, so there's no reason why the Get Rule would not apply here. The Quartz 2D Documentation also specifically reiterates that the CoreFoundation memory management model applies to the Quartz 2D API.
I'm 100% unfamiliar with coding anything Apple related, but I'd imagine that the cgctx has nothing to do with the data pointer, so you would have to free it on its own. IE The CGContextRelease() doesn't clean up the pointer.
链接地址: http://www.djcxy.com/p/53106.html上一篇: 通过发送短信打开Android应用程序?
下一篇: 为什么我必须释放这些数据? 我是店主吗?