On Wed, Aug 20, 2008 at 9:37 AM, Joan Lluch (casa) <[EMAIL PROTECTED]> wrote:
>
>>
>> Am Mo,18.08.2008 um 20:14 schrieb Andy Lee:
>>>
>>
>> Since the observed object (maybe notification center) will still hold a
>> reference to the observing object, there is a problem. The *observer* will
>> not be freed, if the observed is still living. Obviously you have a problem
>> using GC,  for example if you have an observing window and an observed model
>> instance. The model probably lives longer than the window. (Think of info
>> windows …)
>>
>> This seems to be the reason for the weak reference using GC.
>
> I've been reading this thread and I don't understand it in the case of GC.
> Why don't you still have to remove the observer from the notification center
> when you are not longer using it. Even if it is held as a weak reference, it
> will still remain registered for receiving notifications, so what when the
> observer is eventually collected, won't the notification center continue
> sending messages to the observer as long as it remains registered to receive
> them?, Please clarify.

"Weak reference" is a bit of an abbreviation in this case. What
NSNotificationCenter uses (and what you should always use if you want
a weak reference in GC land) is a *zeroing* weak reference.

To illustrate, here is how you create a zeroing weak reference:

__weak id someRef;

Now you can use this just like any other variable:

someRef = myObj;

The trick is that when the object that it points to goes away, someRef
gets zeroed out:

id localObj = someRef;
if(!localObj) // the object was destroyed!

The NSPointerArray, NSHashTable, and NSMapTable classes allow you to
use zeroing weak references as part of a collection. For example,
NSNotificationCenter could use an NSMapTable with weak objects, and
then any time an object in the table gets destroyed, its entry
disappears and NSNotificationCenter will no longer message it.

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 [EMAIL PROTECTED]

Reply via email to