lua-users home
lua-l archive

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


On Mon, Jul 28, 2008 at 5:15 AM, E. Wing <> wrote:
> I can modify the vector later and its changes will be reflected in the
> matrix which is good. But I realized that the matrix can get released
> by the garbage collector even though there might still be vectors that
> have a pointer to it. So if I do this:
> my_matrix = nil
> collectgarbage()
> print(my_vector1) -- I expect garbage or crashes

I would be hesitant to say this is a good thing. From a C perspective
it looks natural, but if this is to be used by a script writer (e.g.
someone who uses tools like MatLab), this may seem very strange. In
Matlab, they make copies of a matrix/vector when you reference it.
There are places where you cannot simulate this entirely in Lua (e.g.
passing the matrix to a function); but, having a vector still
reference a matrix is odd.

If you still intend to have this behavior, then I would suggest:

1) Creating these vector userdata immediately when making a new matrix
and keep them in the matrix environment table (you need to also have
the matrix reference itself somehow in the environment table). Having
each vector have an environment table could become costly.

2) Make a table with some reference in the registry with weak keys and
strong values. Associate each vector (key) to its matrix (value). When
all the vectors are collected then the matrix itself will be
collectible (I would prefer this tbh; it incurs little cost compared
to environment tables).


-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant