[Haskell-cafe] Attach a finalizer to a FunPtr

Zemyla zemyla at gmail.com
Tue Sep 17 14:22:49 UTC 2019


Yeah, the restriction on attaching a finalizer is that the value it's
attached to has to be boxed, not that it has to be lifted. A ByteArray#
with the Addr# stored inside could have a finalizer attached.

On Tue, Sep 17, 2019, 03:40 Matthew Pickering <matthewtpickering at gmail.com>
wrote:

> It is my understanding that you can attach a weak pointer to a MurVar#
> or a ThreadId# because they are implemented using the standard closure
> layout, ie an info table pointer followed by a payload. Addr# is not
> represented in the same way.
>
> Cheers,
>
> Matt
>
>
> On Tue, Sep 17, 2019 at 9:27 AM Roman Cheplyaka
> <roman.cheplyaka at tweag.io> wrote:
> >
> > On 17/09/2019 10.50, Shao Cheng wrote:
> > > Hi Roman,
> > >
> > > mkWeak# 's "key" and "value" arguments must both be lifted types, see
> https://gitlab.haskell.org/ghc/ghc/blob/master/rts/PrimOps.cmm#L699. Your
> code directly passes the raw Addr# which is not a managed pointer, thus the
> segfault.
> > Ah, that makes sense — thanks Cheng (and Matthew). I modeled my code
> after mkWeakIORef, and I assumed that MutVar# and Addr# were in the same
> category.
> > > I recommend using ForeignPtr in this case. You can add finalizers to
> ForeignPtr properly, and coerce from ForeignPtr to FunPtr when using it,
> since they're both containing the Addr# under the hood.
> >
> > I considered doing that, but that'd require quite a bit of code to be
> constantly converting between ForeignPtr and FunPtr and also converting IO
> () finalizers to FunPtr (...) finalizers. System.Mem.Weak looks like a
> better fit because it uses IO () finalizers.
> >
> > I think I'll try attaching a finalizer to ForeignPtr itself and be
> careful to retain a pointer to ForeignPtr (perhaps with the help of a
> StablePtr) while it's in use. Or, if that doesn't work, I might implement a
> reference counter instead.
> >
> > Roman
> >
> > _______________________________________________
> > Haskell-Cafe mailing list
> > To (un)subscribe, modify options or view archives go to:
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> > Only members subscribed via the mailman list are allowed to post.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190917/813f5ae7/attachment.html>


More information about the Haskell-Cafe mailing list