Weak References

Mark S. Miller erights at google.com
Mon Sep 7 17:23:43 UTC 2015


On Mon, Sep 7, 2015 at 10:19 AM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:

> Resending this as a new message because of my apparently broken
> WYSIWYDG (what you see is what you don't get) email client... :-(
>
> Hopefully this doesn't itself get corrupted...
>
> > 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
> > function rotateStream(interval, format) {
> >     const res = {}
> >     setStream(res, format)
> >
> >     let stream = new WeakReference(res) , format)
>

Unbalanced parens.


> >
> >     let timer = setInterval(() => {
> >         setStream(stream.get(), format)
> >     }, interval)
> >
> >     stream.onCollect(() => {
> >         // reference is dead, clear this timer
> >         clearTimer(timer)
> >         // break local strong references
> >         timer = stream = null
> >     })
> >
> >     // Return a strong reference
> >     return res
> > }
> > ```
> >
> > 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)
>
> (it got corrupted when saving somehow :-( )
>
>
> On Sun, Sep 6, 2015 at 11:34 PM, Mark S. Miller <erights at google.com>
> wrote:
> >
> >
> > 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
>
>
>
> --
> Isiah Meadows
>



-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150907/c73715d2/attachment.html>


More information about the es-discuss mailing list