• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)


  • Subject: Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)
  • From: Michael Ash <email@hidden>
  • Date: Wed, 7 Oct 2009 18:01:56 -0400

On Wed, Oct 7, 2009 at 5: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?

The purpose is to decrement the ref-count by 1, not necessarily to 0,
in the GC world, and leave it alone in non-GC. To put it more simply,
CFMakeCollectable is just, if(GC) CFRelease(obj).

>> 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 );

Since CFMakeCollectable does CFRelease under GC and nothing under
non-GC, your code, paraphrased, is like this:

"If we're not under garbage collection, then CFRelease the object. If
we're under garbage collection, then CFRelease the object."

This is obviously a bit redundant. Much simpler to just use CFRelease
with no conditionals. This is valid under both GC and non-GC.

> Or does CFMakeCollectable() always have to be used like this?
>
>    CFTypeRef obj = CFMakeCollectable( CFCreateType( ... ) );

The major use of CFMakeCollectable, and its friend NSMakeCollectable
(which does the same thing and just has a different declared return
type), is to give reasonable "autorelease" semantics to
toll-free-bridged objects in dual-mode code. For example, where you
would previously have written something like:

NSString *str = [(id)CFStringCreateFoo() autorelease];

For dual-mode code you would write:

NSString *str = [NSMakeCollectable(CFStringCreateFoo()) autorelease];

And for GC-only code you could write:

NSString *str = NSMakeCollectable(CFStringCreateFoo());

Or simply:

NSString *str = (id)CFStringCreateFoo();
CFRelease(str);

For pure CF code that never touches Objective-C, no changes are required.

I imagine you've read it already, but if not, there's a good
discussion on CoreFoundation usage under garbage collection here:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/GarbageCollection/Articles/gcCoreFoundation.html

Mike
_______________________________________________

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

References: 
 >Re: EXC_BAD_ACCESS when -fobjc-gc is on (Was: Memory corruption ...) (From: Gabriel Zachmann <email@hidden>)
 >Re: EXC_BAD_ACCESS when -fobjc-gc is on (Was: Memory corruption ...) (From: David Duncan <email@hidden>)
 >Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc) (From: Gabriel Zachmann <email@hidden>)

  • Prev by Date: Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)
  • Next by Date: Re: Autorelease pool
  • Previous by thread: Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)
  • Next by thread: Re: Understanding CFMakeCollectable (was: EXC_BAD_ACCESS when -fobjc-gc)
  • Index(es):
    • Date
    • Thread