haskell / c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
http://hackage.haskell.org/package/c2hs
Other
199 stars 50 forks source link

default out-marshaller for ForeignPtr adds finalizer for null ptr #234

Open mpdairy opened 5 years ago

mpdairy commented 5 years ago

Sometimes C functions return null pointers. The default marshaller for ForeignPtr's in c2hs still adds a finalizer for null pointers, which causes the application to crash when the memory is freed. I suggest the default marshaller should check if the ptr equals nullPtr and, if so, use newForeignPtr_ instead.

jimenezrick commented 4 years ago

I was also looking into this. In my case, I ended up writing a thin C wrapper function used as finalizer, where I checked if the pointer is not NULL and then I call the function doing the release operation.