What is the status of Weak References?
David Bruant
bruant.d at gmail.com
Sat Feb 2 13:16:49 PST 2013
Le 02/02/2013 20:02, Brendan Eich a écrit :
> David Bruant wrote:
>> Interestingly, revocable proxies require their creator to think to
>> the lifecycle of the object to the point where they know when the
>> object shouldn't be used anymore by whoever they shared the proxy
>> with. I feel this is the exact same reflections that is needed to
>> understand when an object isn't needed anymore within a trust
>> boundary... seriously questioning the need for weak references.
>
> Sorry, but this is naive.
It is, you don't need to apologize.
> Real systems such as COM, XPCOM, Java, and C# support weak references
> for good reasons. One cannot do "data binding" transparently without
> either making a leak or requiring manual dispose (or polling hacks),
> precisely because the lifecycle of the model and view data are not
> known to one another, and should not be coupled.
>
> See http://wiki.ecmascript.org/doku.php?id=strawman:weak_refs intro,
> on the observer and publish-subscribe patterns.
I guess manual dispose would make a lot of sense. A view knows own its
lifecycle, it involves adding observers in a bunch of places. When the
view lifecycle comes to an end for whatever reason, it only makes sense
that it removes the observers it added. My rule of thumb would be "clean
up the mess you made".
Memory leaks are bugs. Like off-by-ones. People should just fix their bugs.
Garbage collectors encourage the fantasy that people can forget about
memory. It is a fantasy. A convenient one, but a fantasy nonetheless. A
fantasy like "we can have a lifestyle that assumes oil is unlimited".
</naivety>
<acceptance>
I guess it's just human nature, so weakrefs are pretty much unavoidable.
If a weakref to a function is passed to Object.observe, will it auto-get
the function and unobserve automatically if the .get returns null?
David
More information about the es-discuss
mailing list