[oolua] C++/OOLUA Memory management

  • From: "apscience" <apscience@xxxxxxxx>
  • To: <oolua@xxxxxxxxxxxxx>
  • Date: Mon, 14 Oct 2013 18:08:39 -0700

Hello,

I’m trying to use OOLUA for a project but I’m afraid I’m kind of stuck.

Essentially I have C++ objects that are created in C++. I want to be able to 
pass them to lua for manipulation. However I don’t want the lua script to ever 
have references to destroyed objects. The objects can be destroyed in lua or 
from within c++. Is there any way to go through all the lua state and set 
references that reference a destroyed object to nil? Or to recognize a passed 
reference is invalid and throw an error on the LUA side? (That way the C++ 
never tries to use an invalid poinetr)

My current approach is to create all the objects in lua and store them in 
arrays. When I want to destroy the object from C++ I remove the the object from 
the array in lua. Then when all the references are eventually destroyed the lua 
code automatically cleans up the object.

Unfortunately this has a lot of down sides:
* Relying a lot on the lua garbage collector
* have to create/destroy everything using lua

I might be able to work around some of the down sides, but at the very least I 
really want to create the objects in C++. So I want to be able to have some 
code that looks like:
object * MakeObject() {
    return new object(stuff);
}

and have the object* be memory tracked by LUA. Or if there’s a way to avoid 
this hacky approach altogether that would be appreciated.

Thanks,
Anthony

Other related posts: