[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How do I get two dimensional array access notation for my (matrix) userdata?
- From: "Patrick Donnelly" <batrick.donnelly@...>
- Date: Mon, 28 Jul 2008 18:50:50 -0600
On Mon, Jul 28, 2008 at 5:15 AM, E. Wing <email@example.com> 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
> 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).
"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."