What is the status of Weak References?

Marius Gundersen gundersen at gmail.com
Sun Mar 3 01:30:59 PST 2013

> In the weakref case, someone has to keep a strong reference to the
listener until it's not needed anymore. The decision to cut this last
strong reference is exactly the same decision as deciding when not to

Unless there are multiple references to an object, in which case you cannot
know when the last strong reference is cut, unless you employ reference
counting. With WeakRefs an object can be a listener, and can be passed
around the application until it is no longer needed, at which point it is
garbage collected and stops listening.

> I think the difference is that it might be that the last reference is cut
because another object got released and this object got released because
another object got released, etc. Unless the entire application is
collectable, I think somewhere in the chain, there is an explicit strong
reference cut.

Yes, this is the exact scenario I have trouble implementing, which WeakRefs
would solve.

> My point is that being garbage collectable needs an explicit action
somewhere. WeakRef is a convenient construct that benefits from a cascading
of the explicit action. But there is an explicit action needed somewhere

> There can be a removeEventListener too.

I don't see why there would be a need for explicit reattaching and explicit
unattaching to an event. If a removeEventListener method exists, then you
can use that method inside the listener function when you want to
unsubscribe to the events, rather than (not) calling the addEventListener
every time.

The explicit reattaching of the rAF works because you know how long it is
until the next animation frame, but it won't work in a system where the
time until the next event is indeterminable.

Marius Gundersen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130303/b6ec7601/attachment-0001.html>

More information about the es-discuss mailing list