News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
table reset
Author Message
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #1
Question  table reset

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?

This post was last modified: 12-04-2010 01:51 AM by mikshaw.

12-04-2010 01:37 AM
Find all posts by this user Quote this message in a reply
jpjacobs
Member
***


Posts: 113
Group: Registered
Joined: Jul 2007
Status: Offline
Reputation: 0
Post: #2
RE: table reset

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 http://www.lua.org/pil/17.html

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:

Code:
w=fltk.Fl_Window(1,1,1,1,"Window")
getmetatable(w).__gc=function(window) print("Bye Bye from window") end
w=nil
collectgarbage()


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.

12-04-2010 11:16 AM
Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #3
RE: table reset

That's what I was hoping for =o)
Thanks.

12-04-2010 01:37 PM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: