I think I figured this out – the issue is with the bare value ocaml_str_in
declaration – this value needs to be declared with CAMLlocal1.
I didn’t use that pattern because I was naively copying the examples from this otherwise good tutorial here: Calling OCaml from C
Figured out the correct pattern thanks to this blog although because this error is subtle it took me a dozen read throughs to notice which mistake I was making Easy mistakes when writing OCaml C bindings - Brendan Long