<div dir="auto">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.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Sep 17, 2019, 03:40 Matthew Pickering <<a href="mailto:matthewtpickering@gmail.com">matthewtpickering@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It is my understanding that you can attach a weak pointer to a MurVar#<br>
or a ThreadId# because they are implemented using the standard closure<br>
layout, ie an info table pointer followed by a payload. Addr# is not<br>
represented in the same way.<br>
<br>
Cheers,<br>
<br>
Matt<br>
<br>
<br>
On Tue, Sep 17, 2019 at 9:27 AM Roman Cheplyaka<br>
<<a href="mailto:roman.cheplyaka@tweag.io" target="_blank" rel="noreferrer">roman.cheplyaka@tweag.io</a>> wrote:<br>
><br>
> On 17/09/2019 10.50, Shao Cheng wrote:<br>
> > Hi Roman,<br>
> ><br>
> > mkWeak# 's "key" and "value" arguments must both be lifted types, see <a href="https://gitlab.haskell.org/ghc/ghc/blob/master/rts/PrimOps.cmm#L699" rel="noreferrer noreferrer" target="_blank">https://gitlab.haskell.org/ghc/ghc/blob/master/rts/PrimOps.cmm#L699</a>. Your code directly passes the raw Addr# which is not a managed pointer, thus the segfault.<br>
> 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.<br>
> > 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.<br>
><br>
> 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.<br>
><br>
> 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.<br>
><br>
> Roman<br>
><br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> To (un)subscribe, modify options or view archives go to:<br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
> Only members subscribed via the mailman list are allowed to post.<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>