[Haskell-cafe] [ANN] Haskell FFI Tutorial

Evan Laforge qdunkan at gmail.com
Mon Nov 10 19:22:42 UTC 2014


On Sat, Nov 8, 2014 at 7:54 AM, Alex Petrov <oleksandr.petrov at gmail.com> wrote:
> If you have any feedback / comments / questions, know how to improve it or
> would like to see more things covered, just ping me.

Just from personal experience, the fact that haskell types like Bool
and Char are Storable combined with hsc's low level nature makes it
easy to create memory corruption bugs by writing `(#poke Struct field)
flag` instead of `(#poke Struct field) (toBool flag)`.  The former
will compile and likely work most of the time, but will corrupt
memory, which will eventually result in a nondeterministic GC crash
some time after the actual corruption.  In my case, it took over a
year to find the bug.

I wound up writing a CStorable class that omits the dangerous
instances, but a better solution is probably to use a higher level
tool that doesn't make you manually specify the types of struct
fields.  I won't make the mistake again, and I wouldn't want to
encourage anyone else to make it either.  If I were writing a tutorial
the very least I'd put in red flashing warnings.


More information about the Haskell-Cafe mailing list