Resetting the kernel when calling newForeignPtr

Next program:

{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import qualified Data.Vector.Storable as S
import Data.Vector.Storable.Internal
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc

main = do
  let (fp,_,_) = S.unsafeToForeignPtr $ S.fromList [1::Int]
      p = getPtr fp
  copy <- newForeignPtr finalizerFree p -- causes core dump
  print "finished"

      

with kernel dumps with the following error message:

"finished"
*** Error in `./test2': free(): invalid size: 0x00007f4dffc06030 ***
Aborted (core dumped)

      

Did I get it right that this is happening because the two data values ​​are pointing to one bit of memory ( p

specifically) that ghc then tries to garbage collect twice? If so, can I make a copy p

? If this is not the cause, what is causing the problem and how can I fix it? Thanks to

+3


source to share





All Articles