Description
Dislaimer: this is not a bug, more like an interface issue, but probably it would be possible to resolve this somehow.
The inverted stubs basically follow the same principle as straight stubs and try to fit into the same interface. However, the interface similarity may hide fundamental differences, as creating a value is very different from consuming a value. Let me ground this with a concrete example. It is very easy to create the following (innocent looking) callback:
internal "lets_crash" Ctypes.(void @-> returning string)
(fun () -> sprintf "%g" (4. *. atan 1.))
The generated stub will drop a reference to the constructed string and after few GC cycles the data would be corrupted or a segmentation fault will happen (for the lucky ones). The same is true for other values that are managed by OCaml (i.e., block values).
It looks like, that the ctypes were originally designed with an idea of consuming values that are coming from C, and occasionally providing values to the C realm but only under a constraint that they are returned from the callback functions (e.g., the sort example). With the inverted stubs, this constraint doesn't hold anymore and returning a value that is represented as a block from the inverted stub is always a bug (unless special precautions were made to extend the lifetime of the value).
I'm not sure, whether the problem can be solved on the implementation side, or on the interface one, but at least it is good to know, that the issue exists. (And probably a big fat warning in the documentation wouldn't make any harm)