Sign in to follow this  
agent_smith666

Calling tables from C help

Recommended Posts

agent_smith666    101
I'm trying to retrieve integers from a table in my Lua script. When I run this code "slowly" (not called often) it works just fine but I'd like to run it in my game loop (which will call it very fast) and when that happens I get an error from MSVC. The code is from the Lua manual (section 25.1) from lua.org. "EAP[Game.exe]: Heap block at 038A88D8 modified at 038A8BB0 past requested size of 2d0 Windows has triggered a breakpoint in Game.exe. This may be due to a corruption of the heap, which indicates a bug in Game.exe or any of the DLLs it has loaded." Is Lua not intended to be called "quickly"? From what I can guess it looks like the stack is increasing in size despite the "lua_pop" being executed every cycle. Also it seems like when I call lua.getglobal a lot I sometimes get this problem. I've tried calling functions in Lua from C in my game loop and it seems to work ok. Here's my C function; for example I would use it like:
display_stats("Player Health: %i", get_player(L, "health"));


int get_player(lua_State *pLuaState, const char *key) 
{
        int result;
        // for now only use the "player" table
	lua_getglobal(pLuaState, "player");
	if (!lua_istable(pLuaState, -1))
	{
		HAPI_error(pLuaState, "`player' is not a valid table");
		return -1;
	}
	else
	{
		
		lua_pushstring(pLuaState, key);
		lua_gettable(pLuaState, -2); 
		if (!lua_isnumber(pLuaState, -1))
		{
                  HAPI_error(pLuaState, "invalid component in player table");
                  return -1;
		}
                else
                {
		  result = (int)lua_tonumber(pLuaState, -1);
		  lua_pop(pLuaState, 1);  /* remove number */
		}  
                
	}
        return result;
}








*** Update *** I put variables to keep track of the stack size (lua_gettop function) and it seems to be growing 1 per cycle. It appears I need to pop something else off but I dunno from what index. *** Update 2 *** I changed the value from "1" to "-1" for the index of the "lua_pop" function and it so far it looks like it fixed the memory leak. I've been up and down the manual and they have for this/their function they have a "1" for lua_pop, perhaps a misprint or am I still in error?
lua_pop(pLuaState, -1);



[Edited by - agent_smith666 on November 11, 2009 5:56:04 PM]

Share this post


Link to post
Share on other sites
Kylotan    9881
You push two things onto the stack, via lua_getglobal and then lua_pushstring. Then you use lua_gettable to replace the key string with the value, which you pop. That leaves the original global in place.

If you wrote a routine to dump the contents of the stack out to a file or to the screen, you could probably work this out very quickly in future.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this