lua-users home
lua-l archive

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


AJ wrote:
> I have encountered a seemingly major problem that I've been trying to
> fix for about two days in a decently (for me--it's around 5,000 lines
> of code plus various Lua scripts) sized project of mine.  The problem
> is that nothing that in a sandboxed chunk is getting collected.  For
> example, I have this example Lua script:    
> 
> Title = {}
> 
> function Title:Initialize()
> 	self.Font = Font.Create("Content/Fonts/Font") end
> 
> Now, I load this file (named `Title.lua' for this example) using
> luaL_loadfile.  I then proceed to create a new environment for this
> chunk (the environment isn't entirely sandboxed--it's just there to
> stop scripts from accidentally overwriting other variables,
> etc--there are still getfenv() and company), set the chunk's
> environment to the newly created table.  I then make a reference to
> it using luaL_ref in a global table, get it back, run it, and then
> put the chunk's environment in the same position in the table (so the
> chunk is discarded).        
> 
> I believe discarding the chunk is fine, because the `Title' table and
> its sole function, Initialize, would be stored in the environment for
> that chunk.  I am not too sure, however, if the chunk must be kept
> around.  I run into no problems calling Title:Initialize (and in the
> actual program of mine, I have many other functions that I have no
> problem with).     

You can safely discard the chunk. If the Title.Initialize closure needs
some bytecode from it, it will be properly referenced and won't be
collected.

> Now, to get rid of it, I simply call luaL_unref on the table with the
> correct reference.  I figured this would remove the only reference to
> the environment, and so it would be collected.  However, I presume I
> am wrong because the font's `__gc' event is never called until I
> explicitly call lua_close on the state at the end of the program, so
> my memory usage skyrockets if I constantly reload `Title.lua' (and
> other Lua scripts that follow the same premise), instead of staying
> around the same.       
> 
> The problem is that the table is not seemingly getting collected (or
> no userdata in the table is), no matter what I try to do.  Is it
> because I discard the chunk, or something else?  

Did you remove all references to the environment table from your Lua
stack ? If your objects are big enough you may reach memory limits
before overflowing the Lua stack and thus not notice it.