Re: ARC + return of autoreleased CFType
Re: ARC + return of autoreleased CFType
- Subject: Re: ARC + return of autoreleased CFType
- From: Roland King <email@hidden>
- Date: Wed, 19 Oct 2011 21:24:59 +0800
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 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!
On Oct 19, 2011, at 9:04 PM, John Pannell wrote:
> Hi all-
>
> I've got a category on NSColor to return a CGColor value. Source looks like this:
>
> - (CGColorRef)CGColor
> {
> 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;
> }
>
> My issue is with the final line: the CGColor has a retain count of 1, as it was made with a "Create" function. Following Cocoa convention, I'd want to cast the return value and autorelease it, but the autorelease call is not allowed under ARC. Xcode is doing its best to help me figure out how to return the value, but the only thing I can get it to not squeak about is this:
>
> return (__bridge_retained CGColorRef)(__bridge_transfer id)theColor;
>
> which does not seem reasonable to me. Can anyone suggest the proper syntax to return an "autoreleased" CGColorRef from this function?
>
> Thanks!
>
> John
>
>
> John Pannell
> http://www.positivespinmedia.com_______________________________________________
>
> 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
_______________________________________________
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