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: "Michael Ash" <email@hidden>
- Date: Sun, 6 Jul 2008 11:35:08 -0400
On Sun, Jul 6, 2008 at 11:25 AM, Bill Cheeseman <email@hidden> wrote:
> on 2008-07-06 9:49 AM, Jean-Daniel Dupas at email@hidden wrote:
>
>> If __strong is so dangerous, why is it used in Cocoa (see NSDrawer,
>> NSDateFormatter, NSNumberFormatter, NSKeyedArchiver, NSURL, Š) ?
>
> Those are interesting examples, especially NSDrawer.
>
> In NSDrawer, only the CFRunLoopTimerRef and CFRunLoopObserverRef iVars are
> given the __strong keyword, while the CFRunLoopRef iVar is not. This
> suggests to me that some very refined thinking is going on as to when to use
> or not to use the __strong keyword. I wish I knew what all the relevant
> considerations were.
CFRunLoopRefs live for the entire lifetime of their thread, so there's
no need for __strong.
But I would suggest that, as is the case for many other things, you
don't need to duplicate Apple's refined thinking as to whether to use
__strong or not.
Do you want to keep a reference to a CF object? If yes, use __strong.
End of story.
You might use __weak if you want a zeroing weak reference. But this is
extremely uncommon in a dual-mode framework such as what you're
writing, because there's no equivalent to __weak in the non-GC world.
Aside from wanting a zeroing weak reference, there's no real reason
not to use __strong. The only reason would be for performance, and as
we all know, premature optimization is the root of all evil.
You *can* leave off the __strong keyword, provided you do your
CFRetains and CFReleases properly and implement a -finalize method to
clean things up. But this is less efficient, requires more brainpower,
and is unnecessary.
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