Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)
Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)
- Subject: Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)
- From: Dave Carrigan <email@hidden>
- Date: Wed, 7 Oct 2009 15:09:25 -0700
On Oct 7, 2009, at 2:48 PM, Gabriel Zachmann wrote:
So in other words, the purpose of CFMakeCollectable() is to decrease
the ref-count to 0 in the GC world, and only there, is that correct?
Not quite; it decreases the ref-count by 1 in a gc environment. Every
CFRetain must be matched by a CFMakeCollectable.
Since you do not want to release them in a ref counted environment,
CFMakeCollectable (and NSMakeCollectable) need to do nothing in ref-
counted (or your objects would vanish) and CFRelease (not -release)
in a GC environment.
So, when I have old code like this:
CFTypeRef obj = CFCreateType( ... );
// do something with obj
CFRelease( obj );
I always need to transform it into this:
You don't need to transform it at all. It will continue to do the
right thing in gc and non-gc environments, and you usually don't want
to rewrite working code just for the sake of rewriting it.
CFTypeRef obj = CFCreateType( ... );
// do something with obj
if ([NSGarbageCollector defaultCollector] == NULL )
CFRelease( obj );
CFMakeCollectable( obj );
Or does CFMakeCollectable() always have to be used like this?
CFTypeRef obj = CFMakeCollectable( CFCreateType( ... ) );
For new code, the typical pattern is
CFTypeRef obj = CFMakeCollectable(CFCreateType(…)); // no-op in non-
gc; releases and makes eligible for collection in gc
// ...
if ([NSGarbageCollector defaultCollector] == NULL) CFRelease(obj); //
releases in non-gc only
--
Dave Carrigan
email@hidden
Seattle, WA, USA
_______________________________________________
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