Looking briefly at the Lunar code, I don't see anything that tries to
handle the case of the object being deleted from the C++ side. The only
cleanup appears to be when the object's associated userdata is garbage
collected from the lua side.
Maybe you could always delete the ship from the lua code? This would
require a wrapper function that calls into lua to nil out the reference
to the shup in script, causing it to be garbage collected.
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-
bounces@bazar2.conectiva.com.br] On Behalf Of Watusimoto
Sent: Thursday, July 02, 2009 1:04 AM
To: lua@bazar2.conectiva.com.br
Subject: Lunar, C++, and premature object destruction
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
http://bitfighter.org, 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,
Chris