Fatal error: allocation failure during minor GC

res = caml_alloc_2(Tag_cons, caml_copy_string(r->head), result);

The line above looks dubious. Consider the following scenario. The value result is first passed to caml_alloc_2, then caml_copy_string is called, which potentially triggers a garbage collection. The collector updates the content of the variable result, but its value has already been put aside by the compiler, so it will not be updated. Therefore, the second component of the block allocated by caml_alloc_2 gets filled with garbage.

I suggest calling caml_copy_string separately, so that result is properly updated by the garbage collector, e.g.,

res = caml_copy_string(r->head);
result = caml_alloc_2(Tag_cons, res, result);
free(r->head);
1 Like