Re: Guidelines for Cocoa frameworks supporting garbage collection?
Re: Guidelines for Cocoa frameworks supporting garbage collection?
- Subject: Re: Guidelines for Cocoa frameworks supporting garbage collection?
- From: Quincey Morris <email@hidden>
- Date: Sat, 5 Jul 2008 15:52:09 -0700
On Jul 5, 2008, at 14:23, Bill Cheeseman wrote:
Here's a specific question: My frameworks contain classes that declare
instance variables derived from CFType. For example, CGEventRef and
AXUIElementRef. In reading the "Instance variables" topic in the "Core
Foundation Variables" section of the "Garbage Collection Programming
Guide,"
I see the statement, "If you declare a Core Foundation structure as an
instance variable, the compiler regards it only as an opaque structure
pointer, not as an object.... To indicate that a Core Foundation
structure
should be treated as a collectable object, you use the __strong
keyword."
The example given declares a CFType-derived object as an instance
variable
in the interface part of a class like this:
__strong CFDateRef myDate;
So, I guess my frameworks should declare ...
__strong CGEventRef myEvent;
__strong AXUIElementRef myUIElement;
... if I am going to advertise the frameworks as supporting both
retain/release and garbage collection. Is that right?
Just to emphasize what Chris said:
With GC on, the CFRetain/CFRelease really do retain and release the
same as with GC off. That *might* suggest the idea that don't need
__strong instance variables. However, with GC on (assuming you went
ahead and called CFMakeCollectable), the __strong instance variables
in effect sorta serve as the replacement for autorelease functionality.
So, in dual-mode code, a CF object will typically be kept alive by its
reference count, maintained as usual in setter methods. Only after a
CFRelease takes the reference count from 1 to 0 does the __strong
reference matter: it will keep the object from (potentially)
disappearing at once. Normally, autorelease has that protective effect.
The other thing worth noting is that, when GC is enabled, any CF
object that is documented to be *returned* already autoreleased from a
frameworks function is actually returned with a reference count of 1,
so you still need to call CFMakeCollectable yourself in that case,
even though you wouldn't follow it with a call to autorelease like you
would in Chris's examples.
If I've said anything wrong here, I'm sure Chris will jump in and
correct me. :)
_______________________________________________
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