lua-users home
lua-l archive

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


Shmuel Zeigerman wrote:
> In my experiments, the following script is running OK by a lua.exe
> that is dynamically linked to lua5.1.dll, but crashes when run by
> a "static" lua.exe. (lpeg.dll is linked dynamically against
> lua5.1.dll).
>
>> require"lpeg"
>> t = {}
>> p = function() t[1]=1 end --> t[1]=1 causes crash
>> lpeg.match(p, "hi")
>> print "OK"

Your analysis is correct. And I can point you to the root of the
cause (well, two of them): ltable.c, line 401, function newkey():

  if (!ttisnil(gval(mp)) || mp == dummynode) {

Or ltable.c, line 375/376, function luaH_free():

  if (t->node != dummynode)
    luaM_freearray(L, t->node, sizenode(t), Node);

Here dummynode is a reference to a static variable and the values
it's compared to come from a heap-allocated structure.

If you link two Lua VMs to a single process then you'll have two
kinds of dummynode with different pointer values. This makes the
above comparisons fail if a table is passed from the 'other' Lua
VM. This subsequently screws up some internal structures.

Note: there's nothing wrong with the Lua source code and there's
no other way to fix it other than fixing linkage.

Morale: always link only _one_ Lua VM into your process.
(You still can open multiple Lua universes with it.)

--Mike