I am getting that data from an CGImageRef and then setting a structure I created to point to this data:
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
const UInt8 * imageData = CFDataGetBytePtr(data);
myPix->data = (l_uint32 *) imageData;
However I am getting an memory leak warning when I do Product -> Analyze.
I think the reason is because I don’t do CFRelease(data). However if I do that myPix -> data gets modified. How can I get rid of the memory leak warning but get it to work properly?
you’re just assigning a pointer there – casting away const (undefined behavior if you write to it), and disposing of the owner (freeing the allocation assigned to
data— also undefined behavior when you read or write to that memory region).if you want a mutable copy of that data, you must create your own mutable copy. the you can safely call
CFRelease(data).there are two immediate ways to create a mutable copy:
1) using
mallocmallocdatato your buffermyPix->datamyPix->datareferences, callfree(myPix->data);2) Create a mutable copy of the CFData.
when you are done with the allocation
myPix->datareferences, callCFRelease(mutableData);