[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: segfault in Lua library
- From: Anthony S <ast_dev@...>
- Date: Thu, 15 Nov 2012 04:15:35 -0800 (PST)
I have a C++ program that is linked with Lua and executes external Lua scripts and sometimes loads and accesses multidimensional Lua tables.
I was encountering strange segfaults in different places, but was not sure if problem is in Lua code.
Now I've written test program that repoduces my program's behavior and calls functions from Lua library exactly in the same way and segfault in Lua code is reproducible on
*nix systems (tested on Gentoo Linux x86_64, Fedora 16 x86, Mac OS X x86_64, Solaris on sparc) and is not reproducible on Windows 7
test program - bug2.cpp
http://pastebin.com/Sq65SY92
test lua file - components.lua
http://pastebin.com/GDxv9uA4
put these two files in same directory, compile the program and run:
g++ -c -I ~/lua-5.2.1/src/ bug2.cpp
g++ -o bug2 bug2.o ~/lua-5.2.1/src/liblua.a -lm -ldl
./bug2
backtrace on Gentoo Linux x86_64:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000405458 in lua_getinfo ()
(gdb) bt
#0 0x0000000000405458 in lua_getinfo ()
#1 0x0000000000412290 in luaL_argerror ()
#2 0x000000000041271e in luaL_checkudata ()
#3 0x0000000000419513 in f_gc ()
#4 0x00000000004063ea in luaD_precall ()
#5 0x000000000040661d in luaD_call ()
#6 0x0000000000405c98 in luaD_rawrunprotected ()
#7 0x0000000000406863 in luaD_pcall ()
#8 0x00000000004077ff in GCTM ()
#9 0x000000000040788f in callallpendingfinalizers ()
#10 0x0000000000408bb8 in luaC_freeallobjects ()
#11 0x000000000040cf5e in close_state ()
#12 0x0000000000402d35 in main ()
Pay attention to the commented out lua_pop() calls. If I call lua_pop() every time when value on stack is not needed anymore, test program works fine, without segfault.
But my program is organized in such way that stack is not cleaned immediately. Lua calls are wrapped in C++ class, and stack is cleaned when the object is destroyed.
IMHO this is still semantically correct, at the moment of segfault stack top is 65 and max stack limit 100000 is not reached yet, so the Lua has the bug