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?
Having your own data gives you total control over it, even the raw version. When you have access to this primary information you have a better capacity to make customised and detailed reports. Your employees become more confident because they know they have a wealth of insights they can use to make competent decisions.
People providing personal data have rights to request their own data, seek erasure or amendment to it, and move it between services, under the General Data Protection Regulation (GDPR). Organisations and people providing data often have an obligation to ensure the data they provide is truthful and accurate.
The laws state that “data subjects” — i.e. your customers — will always maintain ownership over any personal data they share with you. To give data subjects more control over their privacy, the GDPR prescribes the following rights (which your business may need to comply with): The right to be informed.
Data ownership refers to both the possession of and responsibility for information. Ownership implies power as well as control.
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. I.E. The CGContextRelease() doesn't clean up the pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With