lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I'm working on a game that uses Lua to control robot players, written in C++, and bound with Lunar, and am having problems controlling the lifespan of objects created in C++ and referenced in Lua. (It's at, if you are curious, and the linked wiki contains examples of some robot scripts.)

My primary object is a Ship. The lifetime of the Ship object is completely controlled by C++. I also have a proxy object, called LuaShip, that contains a pointer to the Ship, and handles all the requests for information about the Ship from the Lua script. The goal is that when the Ship is destroyed, the pointer on the LuaShip will be set to NULL, and any future requests to the LuaShip can be intelligently handled (probably by generating an error of some sort). The LuaShip is exposed to Lua via Lunar if the robot script requests it (using a getShip() method or somesuch).

The Ship object also contains a pointer to its LuaShip proxy. My problem is that when the Ship object is destroyed, C++ seems to think that all pointers to the LuaShip are gone, and it is OK to destroy the LuaShip as well. It does not seem to recognize when a Lua instance is also referring to the LuaShip. The result is that the LuaShip is destroyed, and the Lua script is now left holding a reference to nothing. When the script acts on it (thinking it's still valid), the app crashes.

My questions are 1) Is this an appropriate architecture for managing Lua instances that can have persistent references to C++ objects (Ship in this case) that can be deleted without warning, and 2) are there any good examples of code that uses Lunar in this manner that I can examine to see where my problem might be?

Thank you,