C - Lua memory issues

Started by
12 comments, last by NicholasLopez 9 years, 8 months ago

the game crashes if I just make everything L


There could be many reasons behind the crash. It's probably one of the last unaddressable access errors in the log. I see invalid printf usage (which probably comes from `lvl_name = get_string(L, "name");`, which could've been freed somewhere) and issues with SDL (though I don't see any SDL code there).

I'm not entirely sure how to do that


Use tables. Store object-specific data in tables, which could then be stored in a global [object_id -> object_table] table. I'm rusty with my Lua API skills as well as I don't quite understand your code so I can't give the exact code for this. But I can say that it involves lua_getfield, lua_setfield, lua_getglobal and lua_setglobal, most of which were already used in your code.

Advertisement

get_string is like this:


char *get_string(lua_State *L, const char *key) { 
	/*	assumes there's a table at -1
		this puts the table[key] value at -1 on the stack */
	lua_getfield(L, -1, key); /* -- (top) table[key] table ... */
	/* get the value at -1 as a string */
	const char* result = lua_tostring(L, -1);    
	/* remove it from the stack */
	lua_pop(L, 1);	
	/* return the value */
	return (char *)result;
} 

and I know it's not an SDL issue, and there are no memory leakages right now. However that is because I commented out the code inside the object functions that pertain to lua. Don't understand what you mean with tables. Isn't there a way to copy the script into a c side variable, and than just have that read from/cleared when it's done?

The problem is not in get_string, it's in the global variable you assign a string with unknown lifetime to. As soon as that string is freed (lua_close at the latest, possibly before that), the variable becomes invalid, it's what people call a dangling pointer.

Don't understand what you mean with tables.

Tables. Tables. Tables. Tables. (click on each link to learn something). At this point it seems like there's no place for not understanding things anymore - if you still don't understand, I suggest practicing with something easier first. If you want to solve the problem, I expect you to know something about Lua tables and be able to look up the things you don't know yourself.

Isn't there a way to copy the script into a c side variable, and than just have that read from/cleared when it's done?

Sure, there is. Compiling the script every time you want to run it will probably be slower than you expect, though.


-- the object
Player = {}

-- ID is set upon object load
ID = nil

-- define the object
function Player:new()
	-- define our parameters here
	local object = {
		x = 0,
		y = 0,
		w = 16,
		h = 32,
		dir = 1,
		xSpeed = 0,
		ySpeed = 0,
		gravity = 30,
		max_vel = 160,
		onground = true,
		jumping = false,
		falling = false,
		jump_held = false,
	}
	setmetatable(object, { __index = Player })
	return object
end

I suppose I can change this first part around again. I understand how to do things, but not the vocabulary they are given. So do I just call the tables creation, and then stuff all of it's contents into a variable in C, and then run the script in my loop process with that table being placed in?

This topic is closed to new replies.

Advertisement