Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lua Using Lunar.

Started by pine3 Jul 17, 2006 at 3:17 AM 5 replies 1k views
Original Post
pine3
pine3
Hi, I have created an object in Lua, but when I get the pointer using lua_touserdata, I'm not getting the same pointer. There is no error though..


static Test* GetObject(lua_State *L)
{		
lua_getglobal(L, "test");

int index = lua_gettop(L);			
Test *pTest = (Test*)lua_touserdata(L, index);

return pTest;
}

// lua
test = Test:new()
test:doSomething()



Thanks in advance!
Quasimodem
Quasimodem
The 2 most important functions in Lunar are check() and push(), the former of which should use to get objects created in Lua into C. Using, check(), the routine you posted would look something like:

static Test* GetObject(lua_State* L) {  lua_getglobal(L, "test");  Test* pTest = Lunar<Test>::check(L, lua_gettop(L));  return pTest;}


Give this a whirl, I hope it works!
pine3
pine3
It works perfectly!

Thanks Quasimodem, I really appreciate your help
Quasimodem
Quasimodem
No problem!

One warning about a pitfall you may encounter: on LUA's next garbage collection cycle, it will likely clean up this test object, meaning the pointer returned by GetObject() will be invalid. The way I worked around this was to use the copy constructor to make a copy of this object that you I could then manage from C. For example,

static Test* GetObject(lua_State* L) {  lua_getglobal(L, "test");  Test* pTest = new Test(*Lunar<Test>::check(L, lua_gettop(L)));  return pTest;}


But now you have to worry about deleting pTest when you are done with it to avoid memory leaks. However, this is certainly more desirable than having random crashes when LUA yanks the rug out from underneath you by deleting your data when you're not ready!
pine3
pine3
Hi Quasimodem,

When will the pointer be garbage collected by Lua? If I left the lua_State open, will the pointer be still garbage collected?

Thanks!
Quasimodem
Quasimodem
LUA maintains some garbage collection rules that you can set from lua_gc() in C or collectgarbage() from LUA. These rules basically tell LUA to run the garbage collector whenever its memory allocation grows to a certain size, for example, every time it doubles, triples, etc.

When the GC runs, it will clean up all objects that LUA knows about that are no longer accessible from within LUA, that is, objects that have no more references in LUA, such as your Test object, regardless of whether your lua_State is still open or not.

LUA does also have a confusing thing called Garbage-Collection Metamethods where you can "tag" your userdata with a special field called "__gc" that will tell the GC not to collect it and that you will arrange a collection later, via C, when you are done with the object. If you feel like delving into this, I would read up on it in the LUA user manual, otherwise, I have found it a lot easier to use the copy constructor and let LUA clean up whatever it wants.

Note that the copy constructor way of doing things is less efficient, you have the overhead of allocating memory twice for basically one object, and until LUA's GC runs, you are using twice the memory you need to be for these objects. But unless you are allocating tons of huge objects and have a deadly need for excellent performance in this area, I wouldn't worry about it :D You have much more fun stuff to spend you time on than boring memory allocation!
pine3
pine3
I see. But it is also possible that the copy constructor have an invalid pointer. I guess I have to try it first though.

Thanks Quasimodem

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.