On Wed, Oct 7, 2009 at 5:48 PM, Gabriel Zachmann <z...@tu-clausthal.de> 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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com