Weak References

Mark S. Miller erights at google.com
Mon Sep 7 03:34:55 UTC 2015


On Sun, Sep 6, 2015 at 8:04 PM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:

> My original email featured a use case, although I had to rewrite it to use
> explicit garbage collection. I need a way to watch for when the object is
> garbage collected.
>
> ```js
>
[...Somehow reformatted in transit...]


> ```
>

Hi Isiah, the text in the above code block somehow got reformatted in
transit. You can see the problem at
https://mail.mozilla.org/pipermail/es-discuss/2015-September/044141.html
https://esdiscuss.org/topic/weak-references#content-10

Since the code below does not have the callback, I don't yet have enough
context to understand your message. When the GC notifies that the stream is
to be collected, by notifying some callback, why does that callback need to
access the stream? What does the callback do with the stream during the
notification that the stream is condemned?

Is the interval callback somehow related to the GC notification callback?
As you see, I just don't get it yet.

>
> The complicating part is that I also need the interval callback to weakly
> reference the value. Otherwise, I've always got one active strong reference
> to the stream. This is with a listener for when the value is garbage
> collected.
>
> Here's the version without a listener (it's simpler, but more crude):
>
> ```js
> function rotateStream(interval, format) {
>     const res = {}
>     setStream(res, format)
>
>     let stream = new WeakReference(res)
>
>     setInterval(function () {
>         try {
>             setStream(stream.get(), format)
>         } catch (e) {
>             // reference is dead, clear this timer
>             clearInterval(this)
>
>             // break strong reference to weak one
>             stream = null
>         }
>     }, interval)
>
>     // Return a strong reference
>     return res
> }
> ```
>
> Sorry for the ambiguity.
>
> (edited out a few bugs from the original)
>
>
>
-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150906/7a4ca9e2/attachment-0001.html>


More information about the es-discuss mailing list