I\'m having a memory leak when using this custom method which returns a CGImageRef. I can\'t release \"cgImage\" properly because I have to return it. What chould I do ?
You can autorelease a Core Foundation-compatible object. it just looks a bit wonky. :)
The GC-safe way is like so:
CGImageRef image = ...;
if (image) {
image = (CGImageRef)[[(id)image retain] autorelease];
CGImageRelease(image);
}
The shortcut, which is safe on iOS but no longer safe on the Mac, is this:
CGImageRef image = ...;
if (image) {
image = (CGImageRef)[(id)image autorelease];
}
Either one will place the image in an autorelease pool and prevent a leak.