Murga-Projects Forums
table reset - Printable Version

+- Murga-Projects Forums (
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: table reset (/showthread.php?tid=395)

table reset - mikshaw - 12-04-2010 01:37 AM

I'm working on a script that loads external scripts into it, and trying to figure out the best way to handle arbitrary variable names and memory usage. What I have at the moment is a table in the main script into which the external script loads its variables and functions.

In order to reset that table's contents to nil in preparation for the next loaded script, I could use a loop to remove all of its elements individually. Or I could simply recreate the table as a new empty table (overwriting the previous one). What I'm wondering is does the latter approach clear the previous table's contents from memory? I'm not sure it does...for example, if you create two widgets which have the same variable name, both of those widgets remain visible and interactive even after a redraw. Maybe there's a better way than either of these two ideas?

I've found that assigning the table's name to a new empty table causes the old table.var to return nil, but does that mean the old data is necessarily purged from memory?

RE: table reset - jpjacobs - 12-04-2010 11:16 AM

really doing something like t={} where t previously contained the table loaded by the function, effectively disconnects the table (value) from the reference (t), and if there aren't any other strong references to this table, it will get deleted upon the next garbage collection cycle. For a read up on garbage collection and strong and weak references, take a look at

I however don't know how the widgets work with this system, whether there remains any reference in Lua. It seems to me, that normally, if everything is implemented in the Lua-way if there aren't any live references to this object it should be removed upon garbage collection. I think (only learning to use the Lua C-API really recently) that it could be possible murgaLua keeps a reference on the C-side, inaccessible to Lua.

To check what happens to data at garbage collection you could (at least for userdata) set the __gc metamethod to print something. For example:

getmetatable(w).__gc=function(window) print("Bye Bye from window") end

prints out the message.
Don't know however how this behaves when Fl:run() has been activated, but you could use the above trick to verify.

RE: table reset - mikshaw - 12-04-2010 01:37 PM

That's what I was hoping for =o)