How to bind to foreign function returning C struct using Ctypes?

I have defined a structure type using Ctypes. When I create a struct on the OCaml side and pass it to C, it works fine. When I try to bind to a function which returns a value of this struct type, I get a segfault error. What is the proper way to bind to a C function returning a struct and access that struct in OCaml? Could you link to or show an example?

This is for a returned pointer to a struct, but I have some (last time I used it) working code here in GDAL bindings: https://github.com/hcarty/ocaml-gdal/blob/master/src/warp.ml#L23-L86

That code is using the libffi-based ctypes-foreign bindings rather then stub generation. I’m not sure what practical differences there are between the two binding approaches in this case.