views:

325

answers:

2

I need a game state object in lua(not c++ or tied to C++) to manage lights, cameras, objects, events from my C++ engine (the lua objects are seperate entities from c++, pretty much just standard lua tables). I am concerned about how the GC is going to act in removing these objects since they are going to be created and removed on the fly. what is the best way to turn on Output for the GC? I have the lua source embedded in my code...

A: 

Did you read the Lua Manual?

Lua objects are completely hidden from the C++ side so you have to put each Lua object into a special hash table and remove it from there when you destroy the C++ object. If the C++ and Lua objects have the same lifetime you can simply do this code in the ctor/dtor.

If you want some debugging output for the GC i guess you have to add printf lines directly into the lua source code as there is AFAIK no code for this in the standard Lua codebase.

Lothar
+4  A: 

The Lua garbage collector doesn't have any output. You have two choices here.

First, you could provide Lua a custom allocator. This would let you track how and when Lua allocates and deallocates memory. This would tell how how often the GC asked for more memory and how often it frees said memory. You could also add allocation tracking that would let you detect 'leaked' memory at application exit.

Since you have the source, you could also add instrumentation to the Lua garbage collector code directly. It's self contained in lugc.c.

Aaron Saarela
Nice answer! that is exactly what i was looking for.
Joe