lua-users home
lua-l archive

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


Hi,

Didn't know you were refcounting stuff. I tried that once, while writing
a raytracer  that described  its scenes in  Lua. C++  objects refcounted
their relations, and Lua had its own bc, of course. It was not a serious
project though, and I didn't have to worry about memory leaks as long as
it didn't crash. But I vaguely remember it worked.

> I do understand you're approach and it does work, but it assumes that 
> Lua "owns" the object, which is not the case in this instance. I really 
> need the ability to store both weak and strong references, which I can 
> get with a pair of tables in my scripting interface (although...well, I 
> think I made my case earlier).

What if you increment your object's refcount  by one when you first pass
it to Lua to take into account that  Lua has a reference to it? Lua's gc
would then  only need to decrement  the refcount by one.  Then, when the
count  reaches zero,  the  C++ object  releases the  strong  ref to  the
associated  table, so  that  the table  can  also  be collected,  before
scheduling itself for destruction ('delete  this' is probably not a good
idea). The table  can't be collected before Lua  collects the associated
object's userdatum since  the object has a strong ref  to it. The object
refcount cannot reach zero before Lua collects it, since Lua incremented
it once.

Nobody owns anybody that  way, and it might work. If  it doesn't, my old
raytracer has a leak :-)

> Thanks for knocking this around with me, I hadn't considered your 
> approach before and it did get me thinking.

Yes, I guess I am on a nerdish  mood today :-). Anyways, I seldom answer
something verbosely, and I thought I needed to make myself clearer.

Best regards,
Diego.