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: Shawn Erickson <email@hidden>
- Date: Wed, 7 Oct 2009 14:59:50 -0700
On Wed, Oct 7, 2009 at 2:48 PM, Gabriel Zachmann <email@hidden>wrote:
> Thanks for your response.
>
> No. CFRetain & CFRelease continue to work the same regardless of GC. That
>> is, the reference count field still exists, but Obj-C objects in GC start
>> life with a 0 retain count and -retain/-release/-retainCount/-autorelease
>> are no-op'd. CF objects still start life with a retain count of 1, and thus
>> you need to release them in order for them to participate in GC.
>>
>
> 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?
>
> 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:
>
> CFTypeRef obj = CFCreateType( ... );
> // do something with obj
> if ([NSGarbageCollector defaultCollector] == NULL )
> CFRelease( obj );
> CFMakeCollectable( obj );
Consider leveraging the asymmetry of CFMakeCollectable and -[NSObject
release] in GC/non-GC worlds... assuming you truly need to write code that
can would in retain/release and GC environments.
CFTypeRef obj = CFMakeCollectable(CFCreateType(...));
// use obj
[(id)obj release];
...or...
CFTypeRef obj = CFCreateType(...);
// use obj
[NSMakeCollectable(obj) release]; or [NSMakeCollectable(obj) autorelease];
-Shawn
_______________________________________________
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