bachle26
bachle26

Reputation: 89

Method returns a Core Foundation object a +1 retain

Can anyone tell me what is the problem I am having here?

Screenshot

Upvotes: 3

Views: 626

Answers (3)

Michael Dautermann
Michael Dautermann

Reputation: 89509

Even though you might have ARC enabled, that only covers Cocoa / Objective-C objects. Core Foundation and Core Graphics API's are different and you still need to eventually explicitly release the memory for any of those objects created as well.

In your code, you are doing a "createCGIImage:" without a balanced release.

A release looks like:

CGImageRelease(myImageRef) 

So, to do this right, do something like:

CGImageRef myImageRef = [context createCGImage: outputImage fromRect: outputImage.extent];
UIImage * imageTemp = [UIImage imageWithCGImage: myImageRef];
CGImageRelease(myImageRef);

Upvotes: 6

Tobi
Tobi

Reputation: 5519

The [context createCGImage:fromRect:] method returns a retained CGImageRef. The fix your memory leak, you need to save a reference to that CGImageRef and release it, when you're done with it:

CGImageRef imageRef = [context createCGImage:outputImage fromRect:outputImage.extend];
imageTemp = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

Upvotes: 1

Rog
Rog

Reputation: 18670

ARC is irrelevant as you are dealing with a CoreFoundation object. You need to assign it to a CGImageRef variable and then release it with CFRelease(variableName).

i.e.

CGImageRef cgImage = [context createCGImage:...];
// rest of your code here
CFRelease(cgImage);

Upvotes: 1

Related Questions