Re: Avoiding mutual retain cycles
Re: Avoiding mutual retain cycles
- Subject: Re: Avoiding mutual retain cycles
- From: "Michael Ash" <email@hidden>
- Date: Mon, 21 Jul 2008 23:54:55 -0400
On Mon, Jul 21, 2008 at 11:24 PM, Markus Spoettl
<email@hidden> wrote:
> On Jul 21, 2008, at 7:23 PM, Michael Ash wrote:
>>
>> Without investigating things more deeply, just from the basic stuff I
>> know about the classes in question, I'd assume that the
>> NSCollectionView owns the NSCollectionViewItems and the views, and the
>> NSCollectionViewItems would also own the views. Where is the retain
>> cycle?
>
>
> Well there is none in the default setup, of course. But here's a non
> far-fetched example to easily create one.
>
> If you're implementing a custom NSView that for some reason has to have
> access to the NSCollectionViewItem or NSCollectionView it belongs to, the
> only proper way to do so (in the collection view world) is by accessing the
> NSCollectionViewItem which has a -collectionView method that returns the
> collection view.
>
> So you need a way back and this you do with a pointer to the
> NSCollectionViewItem in a suitable place. As you don't know the ownership
> circumstances of NSCollectionViewItem and its view the safest thing to do is
> to retain the NSCollectionViewItem in the view. There's the retain cycle,
> because - as it turns out - the NSCollectionViewItem seems to retain the
> view.
>
> The point of all this is not that it's avoided easily - which it is. The
> point is that you can't know the circumstances unless it's documented and
> the original question was how AppKit does avoid things like that.
I guess I misunderstood, I thought you meant that there was already a
cycle there, or at least that it appeared to have one.
I agree that it's not necessarily immediately obvious. But if you
think about it, it should become clear. The NSCollectionViewItem has
to stick around somehow, so that things can access its
representedObject, use it as a target for controls, etc. Therefore
something in there has to be hanging onto it already, which means you
don't have to (and probably shouldn't, because you might make a cycle,
as you've seen). So it does require some thought, but I believe the
necessary information is there if you dig.
> Now that I read more on retain cycles it's clear there is no ingenious Cocoa
> solution to this problem, just plain old defensive programming techniques -
> which is what I was wondering about.
Well, clearly defined ownership helps a lot, but you're right, there's
no special sauce to fix it.
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