lua-users home
lua-l archive

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


I wonder if anyone can help me with the following questions. I am trying to add metamethods like _add and __mul to a lua class (a table configured in the appropriate way). Each of these arithmetic metamethods must return a new object. For example, suppose B and C are objects and I use
 
    A = B + C
 
The result A is a new object, which is fine. I can delete A and grab its memory whenever I want to. Now what if I use an _expression_ like the following?
 
    A = (B + C - D) / D - F*G ^ H
 
There are a whole bunch of intermediate objects created as each operator is processed, but I can never get to them to delete them. When do they get garbage collected? Is it automatic, right after the _expression_ is evaluated?
 
That's the first question.
 
Related to that is a second question. The class holds a pointer to a C++ object. So, when the table (class) gets gc'd, it needs to delete that C++ object as well.
 
I've gone back and forth in the blue book trying to understand this and I just don't get it from the discussion and the examples. I can see that *something* has to have a metamethod __gc which calls a Cfunction with userdata containing the C++ pointer. But the question is the linkage between all these things. I just don't see what to do. Whenever lua tries to collect the table, I want it to call a Cfunction with the pointer that should be deleted.
 
Michael