Following discussion in yallop/ocaml-ctypes#571 (comment) , I am wondering if the following assumption about Sys.opaque_identity is true:
The argument to Sys.opaque_identity cannot be collected as garbage.
The reason I ask is detailed in the above discussion, but briefly we are looking for a way to ensure that particular memory locations are not garbage collected until at least a specific point in a program when using Ctypes. For example, we would like to ensure the following code is safe:
let strchr = Foreign.foreign "strchr" (ptr char @-> char @-> returning (ptr char)) in
let p = CArray.of_string "abc" in
let q = strchr (CArray.start p) 'a' in
let () = Gc.compact () in
let () = ignore (Sys.opaque_identity (p, q)) in (**** This is the important line ****)
Printf.printf "%c\n" !@q
There remains some question if it is, and perhaps the documentation should be updated with information in whichever case it is, as it seems important and the folk-lore is not established in the community.
I tried to check the source, but it's in sys.ml.in and I couldn't trace it further back than that.
CC @yallop @edwintorok for interest.