lua-users home
lua-l archive

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

2008/8/15 Hans van der Meer <>:
> In my previous mail to the list I posed questions about having a
> segmentation fault on using dynamically loaded modules:
>> My question: can anyone more versed in the internals of Lua tell me if
>> this could indeed be the case?
>> Is there a solution or must I absolutely refrain from calling other
>> modules during the gc cleanup phase?
> Some further research revealed more cases where faults arise. As far as I
> can see my tests indeed seem to point at premature removal of my userdata
> items (from my point of view, of course) by the garbage collector.
> The cause could be that the userdata were made in C-code with
> lua_newuserdata. I have a doubly linked list of these that got
> garbage-collected while an operation on the list was still running. Although
> the holder of the chain resides in lua-code, the chain is held entirely by
> C-pointers therein: no special significance for Lua, I am afraid. It might
> well be possible that the memory system doesn't know enough about my data
> structures to be able to keep them as long as needed.
> Therefore I think I can narrow the problem to the following question: if I
> allocate userdata with lua_newuserdata and keep them through C-pointers
> only, how do I let the Lua-system know when to collect and when not?

Lua has no knowledge of your C pointers. You have to keep a Lua
reference to your userdata to prevent it from being collected. To do
that, you can put it in the Lua registry as long as you need it, and
remove it from there once you no longer need it.