You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to lua-l
Hello,
I am getting a segfault on a Lua binding to a C library I wrote. I
traced down the segfault to garbage collection, when two objects that
depend on one another are freed in the wrong order.
I have a Graph object and a GraphIterator object. The latter is a C
structure with a pointer to a Graph. The segfault happens when the __gc
function for the GraphIterator tried to access the Graph after this has
been garbage-collected already.
To avoid this race condition, I have been thinking about adding a Lua
reference to the original Graph in the newly created GraphIterator; I
suppose that then the garbage collector will have to free the
GraphIterator first.
This was my first attempt, which did not succeed:
// Create an iterator from a graph
static int l_graph_add_init (lua_State *L)
{
VOLK_Graph *gr = *(VOLK_Graph **)luaL_checkudata (L, 1, "VOLK.Graph");
This won't work, as the Lua interpreter tells me that I'm trying to
index a userdata value. I think I still have a confused understanding of
how Lua entities are created in the C API. What would be an effective
way to ensure that the dependent object is freed before the one it
points to?