lua-users home
lua-l archive

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


hi,dear all:
  i use ref-counter in my c++ to lua bind mechanism,  it's say that every c++ object which can be pushed to lua( or construct in lua) has to be an instance of "CRefCounter" or it's dervie, when the first time it's push into lua, the middle layer will call it's obj->AddRef(),  and set the __gc tagmethod as to call "obj->Release()" . so for a c++ object, the LuaVM will have a whole owner ref for  all lua variable reference to it,  and after all lua variables set to nil,  the c++ object's will recieve a Release() call in gc, so no memory leak happens.
 
 but the problem is : suppose a hierarchy structure, a node can have a list of children node, usually used in gui. when i new a node with some children node in c++, it has a owner ref for every child, and when it destructs, it will call every child's Release(). in default, a child is only referenced by it's parent, so will destructs within it's parent's. but if once i have called the parent's "getchild(i)" in lua,  the child will have 2 ref, and when the parent destructs, this child won't get destroyed at once, even if actually i only use it to get some information and have no future use.
 
  though it will finally be gc,  i think it's much better if there is a way to control it's life more accurately, such as:
  
  function on_event( obj )
     local child = obj:getchild(0)
     some_global_var = child:getname()
     child = @nil
  end
 
 the symbol "@nil" is invented by me, it means "set to nil as normal, AND force to gc it". so this child won't get a long life which is not necessary.
 
 is it ok? or some simple solutions for my problem without the so big change to lua ? thanks very much..