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: Jean-Daniel Dupas <email@hidden>
- Date: Sun, 6 Jul 2008 18:05:03 +0200
Le 6 juil. 08 à 17:35, Michael Ash a écrit :
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
I think the rule may be:
use __strong for retained/copied ivars, and nothing for assigned ivars.
There is no equivalent of zeroing ref, but weak ref are really common
in Cocoa. For example, delegate are not retain. the Notification
center does not retain observers, and the drawer probably does not
retain the runloop.
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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