Re: ARC + return of autoreleased CFType
Re: ARC + return of autoreleased CFType
- Subject: Re: ARC + return of autoreleased CFType
- From: John Pannell <email@hidden>
- Date: Wed, 19 Oct 2011 11:47:22 -0600
Many thanks to all who responded. Here's a few resolutions for the archives:
1. CFType: cast to id and autorelease. Verdict: it is OK (under manual memory management).
http://www.cocoabuilder.com/archive/cocoa/215004-autorelease-cgimageref.html
http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/tollFreeBridgedTypes.html
Sending an autorelease to an object or opaque type puts the pointer into a collection that will later receive a release call. CFType can be released. (This has worked for me in practice for quite some time, but I'm looking to move to ARC for a new project).
2. Returning an autoreleased opaque type under ARC. Verdict: Might not even make sense.
Mike Ash's excellent explanations did help clarify things, but in the end, I'm not sure that my original request is even sensible. I was looking to hand over an opaque type that was in some collection scheduled to get released later. I don't think any of the casting annotations amount to this. Mike's only example of bridging in this direction (toward a CFType) was the __bridge_retained to transfer ownership from the system into our hands, which is where I was in the first place returning the CGColor.
3. What to do? Keep it simple: I'll modify the method name to reflect the need to memory manage the return value - CGImageCopy. Here is the final method:
- (CGColorRef)CGColorCopy
{
NSColor *colorRGB = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
CGFloat components[4];
[colorRGB getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]];
CGColorSpaceRef theColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGColorRef theColor = CGColorCreate(theColorSpace, components);
CGColorSpaceRelease(theColorSpace);
return theColor;
}
Thanks for everyone's input!
John
On Oct 19, 2011, at 10:09 AM, John Joyce wrote:
> Check the nice clear tutorial at http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html
> It spells out how to handle CF types in ARC.
>
> On Oct 19, 2011, at 9:52 AM, Wade Tregaskis wrote:
>
>>> Following Cocoa convention you'd want to cast it to what and autorelease it? CGColorRef isn't toll-free bridged with anything. If you have been casting it to id and autoreleasing it you might have gotten away with that before but I don't think it's documented anywhere you can do that with CFTypes in general.
>>
>> You can. It may not be in the docs, but all CF types are also NSObjects (or a subclass thereof).
>>
>>> You could change the semantics of the method to return a CFRetain()ed object and make it the responsibility of the caller to release it (and change the name of the method too to make it clear) or you can create a UIColor with your CGColorRef, then CFRelease() it and return the UIColor.
>>>
>>> Mixing autorelease and CFTypes does't seem like a great idea, but I'm prepared for someone to point out a whole piece of documentation I've never seen, that often seems to happen!
>>
>> No, mixing them is indeed a bad idea. CF doesn't have auto release pools, so anything dealing with CF types is free to not think about them, and in practice often does. I would go with explicitly returning the CGColorRef retained.
>> _______________________________________________
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden