Skip to main content
gamedev.net
🔒 Locked

LUA Garbage Collection with nested objects

Started by Quasimodem Jul 9, 2006 at 10:01 PM 0 replies 750+ views
Original Post
Quasimodem
Quasimodem
Hello, after much searching through the LUA wikis I come here for insight into this problem! My game is currently set up in a nested, XML-like way, that is, each Tile contains a vector of other Tiles. I can create Tiles in LUA thanks to Lunar, and add them to parent tiles, for example:

local parentTile = Tile:new("parentTile");
local subTile = Tile:new("subTile");
parentTile:setChildTile(subTile);

The setChildTile routine uses the copy constructor in order to let C "take ownership" of these objects so if LUA decides to delete them, C is unaffected. And sure enough, LUA does often decide to delete them, calling their destructor:

Tile::~Tile() {
  for(vector<Tile*>::iterator i = _subtiles.begin(); i != _subtiles.end(); i++)
    delete i;
}

The problem occurs when LUA deletes a child Tile out from under its parent, that is, LUA will delete "subTile" first (who has no children) and then delete "parentTile", who thinks he still has a child, goes to delete it, and crashes because it's trying to delete a now-invalid pointer. Any chance anyone has encountered a similar thing? I can't quite yet envision what the solution looks like, I supposed I'd like to have more control over LUA's GC to be able to tell it what to delete and when to delete it. Perhaps each Tile could keep a pointer to its parent and alert him when he's been deleted, this seems ugly to me however. I'm open to any thoughts anyone has! Thank you

Topic Locked

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

Sign in to reply to this topic.