
#13148: Adding weak pointers to non-mutable unboxed values segfaults -------------------------------------+------------------------------------- Reporter: mboes | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by rwbarton): I don't know exactly how `mkWeakNoFinalizer#` works but surely it can only possibly attach a finalizer to a value of ''boxed'' type, that is, a value that is stored as a closure on the Haskell heap. After all the meaning of a finalizer is that it's an action that runs when the object is no longer live after GC. `MutVar#` and `MVar#` are boxed (but unlifted) types, but `Addr#` is unboxed, really just a synonym for `Int#`. It doesn't make any more sense to attach a finalizer to `p#` than it does to attach one to `7#`. See https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/HeapObjects for the difference between unboxed and unlifted. The segfault happened because the weak finalizer code interpreted the bit pattern of `p#` as a pointer into the Haskell heap, while it was actually a pointer into the C heap (produced by `malloc`); and chaos ensued. (Based on this program snippet, it looks like you might just want `mallocForeignPtr`.) But morally the problem isn't really with C vs. Haskell heap but with trying to attach a finalizer to an unboxed value. The documentation you refer to is the documentation for the user-facing System.Mem.Weak module. We could add documentation to `mkWeakNoFinalizer#`, even though the primops are really only intended to be used to implement the base libraries. What would have been helpful? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13148#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler