Re: [Caml-list] C binding and GC interaction: storing values outside the heap

2010-09-09 Thread Paolo Donadeo
On Tue, Sep 7, 2010 at 23:48, Stéphane Glondu wrote: Well... could you provide a full self-contained example? Yes, but it could be confusing due to many other details. In any case: $ git clone http://git.ocamlcore.org/ocaml-lua/ocaml-lua.git $ git checkout

[Caml-list] C binding and GC interaction: storing values outside the heap

2010-09-07 Thread Paolo Donadeo
I'm writing a Lua API binding http://ocaml-lua.forge.ocamlcore.org/ and I have a problem regarding the interaction with the garbage collector. The situation is rather canonical: a particular C data type, the Lua statehttp://www.lua.org/manual/5.1/manual.html#lua_state, is used as argument in all

Re: [Caml-list] C binding and GC interaction: storing values outside the heap

2010-09-07 Thread Stéphane Glondu
Le 07/09/2010 22:58, Paolo Donadeo a écrit : I'm writing a Lua API binding [...] typedef struct ocaml_data { value state_value; value panic_callback; } ocaml_data; [...] /* alloc space for the register entry */ ocaml_data *data = (ocaml_data*)caml_stat_alloc(sizeof(ocaml_data));

Re: [Caml-list] C binding and GC interaction: storing values outside the heap

2010-09-07 Thread Paolo Donadeo
On Tue, Sep 7, 2010 at 23:12, Stéphane Glondu wrote: Why don't you call caml_register_global_root on (data-state_value) as well? This was a solution I tried, but with the additional global root the finalization function was never called by the GC, so it solved the segfault with a memory leak :-)

Re: [Caml-list] C binding and GC interaction: storing values outside the heap

2010-09-07 Thread Stéphane Glondu
Le 07/09/2010 23:23, Paolo Donadeo a écrit : Why don't you call caml_register_global_root on (data-state_value) as well? This was a solution I tried, but with the additional global root the finalization function was never called by the GC, so it solved the segfault with a memory leak :-)

Re: [Caml-list] C binding and GC interaction: storing values outside the heap

2010-09-07 Thread Damien Doligez
On 2010-09-07, at 22:58, Paolo Donadeo wrote: The problem is that, for several good reasons, I need a copy, or a reference, to the OCaml value representing the lua_State (v_L in the code above) inside the Lua state (I mean the C data structure). That creates a cross-heap reference loop and