lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I've also struggled with luaL_ref and luaL_unref in the past. So I will try to help.

These functions create a sort of linked list of free indexes, always starting at the zeroth element of the table.

I would recommend writing your own functions, to meet your specific needs. The original functions are not too complicated, and you can find the source in "lauxlib.c". In your case it should be pretty easy because you can just use a monotonically increasing integer.

P.S. In my situation, I needed references but I also need to iterate over all the references. I asked (and answered) this question on stack overflow:

On Thu, Jul 30, 2015 at 6:47 AM Chris Jones <> wrote:

If one does luaL_ref(), luaL_unref() and luaL_ref() again, there's a fair chance that the same reference number will be handed out twice.

I'm wondering if it would be better if the references handed out by luaL_ref() were only ever given out once? I think it would help track down use-after-free type bugs, where right now you get either confusing data of the wrong type, or take incorrect action on data that happens to be the right type.

Clearly this is only an issue in code that has bugs, but we know that all code has bugs, and I think finding them would be easier if a lua_rawgeti() on a reference that had been released, would be able to return a clear "NO YOU ARE DOING SOMETHING WRONG" error.