Closed vrotaru closed 3 days ago
I think it's better to avoid string
(and other views that let OCaml handle memory management) in these situations. If you want to allocate memory that some C code will later pass to free
then one approach is to bind malloc
and allocate the memory that way.
Or more generally allow the user to create views types where the memory which is allocated on OCaml side is GC-ed, but the one which is passed to the C is not freed when it is not longer reachable.
Motivating example:
I'm trying to pass an OCaml callback to C which returns a string. After that the C code "takes ownership" of the returned string and frees it after use. Then when the
char ptr
holding a reference to string on OCaml side is garbage collected this function https://github.com/yallop/ocaml-ctypes/blob/96e1ac32a5909dbd3477ff86d6c41172b2a935fd/src/ctypes/managed_buffer_stubs.c#L19 is called and it tries to free it again.The result of this double-free is that the program crashes.
So, if that is possible, I want an
unmaged_string
where the finalizer for that custom block is null or does nothing.