Possibly important. The lua docs say this about lua_Alloc()
Note that if you add some dummy code in your foo_lua_alloc() right at the beginning of the function, like this
Code:
int x = 1;
if (addr == NULL && size != 0) {
x = 0;
}
This does nothing but you can set a breakpoint on the line x = 0. The part I highlighted in bold (
ptr is NULL if and only if osize is zero) is apparently not true. Setting the breakpoint you can see that Lua is passing in NULL for addr with size (osize) not zero all the time. So I'm not sure what Lua wants you to do because the documentation seems incorrect. I suspect that your problem is this little bit of code
Code:
if ( addr ) {
temp.data = addr;
temp.size = size;
}
else size = 0;
Because when called by luaL_openlibs() addr is NULL and size != 0 frequently. Of course it's possible that the part I wrote in bold is true and Lua is calling the alloc function blindly but I find that hard to believe. What Lua wants you to do when addr == NULL && size != 0 is a bit of a mystery though. I tried removing the else size = 0, but still segfaults.
I guess my suggestion would be to add that "dummy code", add the breakpoint (in your program addr == NULL && size != 0 is only ever true when called from Lua) and backtrace into the Lua code to see if it sheds any light on things. I'm not compiling Lua myself :P Then again, using the example alloc function from the Lua documentation works fine, so the problem is somewhere in your code probably because your code doesn't behave exactly like realloc(). Not sure