mobearette
mobearette

Reputation: 397

CFMutableDataRef Released But Still Causing a Memory Leak

I have the following code in a class method:

CFMutableDataRef pixelData = CFDataCreateMutableCopy(0, 0,
CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)));

UInt8 const *data = (UInt8 *)CFDataGetMutableBytePtr(pixelData);
int pixelInfo = ((image.size.width * scale * point.y) + point.x ) * 4;
UInt8 alpha = data[pixelInfo + 3];
CFRelease(pixelData);

When I do the analysis with XCode, it shows a "Potential leak of an object" at the end of this method. Since I have released the pixelData, I have no clue where the leak could be. When I use instrumentation, I get a leak caused by CGDataProviderCopyData. As I have read elsewhere, the problem should be solved by calling CFRelease(pixelData); Any ideas?

Upvotes: 1

Views: 796

Answers (2)

Thomas Zoechling
Thomas Zoechling

Reputation: 34263

As you are not storing a reference to the data returned by CGDataProviderCopyData, you are not able to send it a release message later.

As Rob points out, your 2nd copy is not necessary.

This should work and avoid the leak:

CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
UInt8 const* data = (UInt8*)CFDataGetBytePtr(pixelData);

Upvotes: 3

Rob Napier
Rob Napier

Reputation: 299683

You're creating a double-copy. You shouldn't be calling CFDataCreateMutableCopy() here. CGDataProviderCopyData() already does the copy for you (as it says in its name).

Upvotes: 3

Related Questions