Re: autorelease CGImageRef?
Re: autorelease CGImageRef?
- Subject: Re: autorelease CGImageRef?
- From: "Ken Ferry" <email@hidden>
- Date: Wed, 6 Aug 2008 20:07:11 -0700
The hope is that most people are either writing GC code, in which case
CFMakeCollectable is a less scary looking version of CFRelease, or
non-GC code in which case they aren't using the function at all.
However, for people who are writing code that needs to work both ways,
here's how I think of it:
(1) You need to balance the NS reference count and the CF reference
count separately.
(2) NSMakeCollectable transfers one reference from the CF column to
the NS column.
Ken Ferry
Cocoa Frameworks
On Wed, Aug 6, 2008 at 7:44 PM, Peter N Lewis <email@hidden> wrote:
> At 7:48 AM -0700 6/8/08, Shawn Erickson wrote:
>>
>> Likely best to point folks at the documentation...
>>
>>
>> <http://developer.apple.com/documentation/Cocoa/Conceptual/GarbageCollection/Articles/gcCoreFoundation.html>
>
> Yes, that is quite helpful (except that the imagined implementation of
> CFMakeCollectable omits the fact that it is a no-op in managed memory mode).
>
> This email sent to email@hidden 2:26 PM +0200 6/8/08,
> Jean-Daniel Dupas wrote:
>>
>> If I'm not wrong, it should be something like this:
>>
>> [NSMakeCollectable(aCGImageRef) autorelease];
>
> This appears correct, except for the fact that, for reasons known only to
> Apple, although CFMakeCollectable is available in 10.4, the trivial
> NSMakeCollectable macro is available only in 10.5. So expanding the
> NSMakeCollectable macro gives:
>
> return [(id)CFMakeCollectable(aCGImageRef) autorelease];
>
> However, the working of this line of code is rather subtle.
>
> In memory managed mode CFMakeCollectable is a no-op and autorelease works,
> so the code is equivalent to
>
> return [(id)aCGImageRef autorelease];
>
> while in garbage collected mode, CFMakeCollectable is equivalent to
> CFRelease, and autorelease is a no-op, so the code is equivalent to:
>
> CFRelease(aCGImageRef);
> return (id)aCGImageRef
>
> The documentation is well worth readings because it is quite tricky the way
> CFRetain/CFRelease always work, even in garbage collection mode, and
> CFRetainCount > 0 stops garbage collection from happening on the object,
> while retain/release/autorelease work only in memory managed mode and are
> no-ops in garbage collection mode (that part is well known I think).
>
> Thanks for the enlightenment!
> Peter.
> --
> Keyboard Maestro 3 Now Available!
> Now With Status Menu triggers!
>
> Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
> <http://www.stairways.com/> <http://download.stairways.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