lua-users home
lua-l archive

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


Hi,

I'm trying to create a lua module for Crystal Space, a 3D framework
<http://crystalspace3d.org> but I have some problems with libdl (dlopen,
dlsym, ...) and lua. The problem is that when lua closes (lua_close
function called) it frees an invalid pointer.

At first, I thought the problem came from SWIG file ... but after
commenting out many parts of the code (and hours of recompilation
because the swig file in itseld is about 6MB) i realized that the
problem was still there even if I disabled completly the SWIG code. So
I created a simple testcase.

My files :

-------------------- luamod.cpp --------------------
extern "C" {
#include <lua.h>
}

extern "C" int luaopen_luamod(lua_State* L) {
    lua_newtable(L);              /* without this line : no errors */
    lua_pop(L, 1);
    return 0;
}

-------------------- testlua.c --------------------

#include <dlfcn.h>
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>

int main(int argc, char* argv[]){
    /* parameters */
    char* lib_str = (argc>=2) ? argv[1] : "./luamod.so";
    char* sym_str = (argc>=3) ? argv[2] : "luaopen_luamod";

    printf("===== Open library and import Lua CFunction =====\n");
    printf("void* lib = dlopen(\"%s\", RTLD_NOW);\n", lib_str);
    void* lib = dlopen(lib_str, RTLD_NOW);
    printf("lib = %p\n", lib);
    printf("error : %s\n", dlerror());
    printf("lua_CFunction f = dlsym(lib, \"%s\");\n", sym_str);
    lua_CFunction f = dlsym(lib, sym_str);
    printf("f = %p\n", f);
    printf("error : %s\n", dlerror());

    printf("===== Create Lua state and call foreign function =====\n");
    printf("lua_State* L = luaL_newstate();\n");
    lua_State* L = luaL_newstate();
    printf("lua_pushcfunction(L, f);\n");
    lua_pushcfunction(L, f);
    printf("lua_call(L, 0, LUA_MULTRET);\n");
    lua_call(L, 0, LUA_MULTRET);

    printf("===== Close Lua state =====\n");
    printf("lua_close(L);\n");
    lua_close(L); /* line 32 */
    return 0;
}

------------------------------------------------------------

My compilation commands :

$ g++ -g -shared luamod.cpp -llua -lm -o luamod.so
$ gcc -g testlua.c -ldl -llua -lm -o testlua

When I run ./testlua :
------------------------------------------------------------
$ ./testlua                              
===== Open library and import Lua CFunction =====
void* lib = dlopen("./luamod.so", RTLD_NOW);
lib = 0x805c018
error : (null)
lua_CFunction f = dlsym(lib, "luaopen_luamod");
f = 0xb7dcd6ec
error : (null)
===== Create Lua state and call foreign function =====
lua_State* L = luaL_newstate();
lua_pushcfunction(L, f);
lua_call(L, 0, LUA_MULTRET);
===== Close Lua state =====
lua_close(L);
*** glibc detected *** ./testlua: free(): invalid pointer: 0xb7ddcbbc
***

------------------------------------------------------------

At the end, I had to do Ctrl-C to interrupt the program as it
apparently ran into an infinite loop (I don't know where, maybe in the
glibc ?)
In some versions of my testcase, I have directly a segfault.

Here is the relevant output of valgrind about this error. I made
some ellipses to fit into lines of 74 char and cut some errors that
happens inside libdl (Conditional jump or move depends on uninitialised
value(s)) :

------------------------------------------------------------
Invalid free() / delete / delete[]
   at 0x4020F9E: free (in /usr/lib/.../vgpreload_memcheck.so)
   by 0x805634E: l_alloc (in .../luabug-testcase/testlua)
   by 0x804DE09: luaM_realloc_ (in .../luabug-testcase/testlua)
   by 0x8051B77: luaH_free (in .../luabug-testcase/testlua)
   by 0x804D58C: sweeplist (in .../luabug-testcase/testlua)
   by 0x804D757: luaC_freeall (in .../luabug-testcase/testlua)
   by 0x8050FB1: close_state (in .../luabug-testcase/testlua)
   by 0x8048E2E: main (testlua.c:32)
 Address 0x428ABBC is not stack'd, malloc'd or (recently) free'd
------------------------------------------------------------

Does anyone can help me in that issue ?
Thanks

Mildred

-- 
Mildred       <xmpp:mildred@jabber.fr> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]