lua-users home
lua-l archive

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


>> do
>>  local upvalue = { --[[ something large ]] }
>>
>>  function my_function() -- global function
>>    print(tostring(upvalue))
>>  end
>> end

>> Upvalue would not be collected until my_function is collected.

> You are right!  I missed that, because I moved most to locals.

> Can I force GC to collect this (from lua)?

> Do I need to do something like "upvalue=nil" at the end of the routine in
> your above example?

You have either to force collect my_function or explicitly set upvalue
to nil. As an option, you may do something like this:

do
 local upvalue = { --[[ something large ]] }

 function my_function() -- global function
   print(tostring(upvalue))
 end

 function close_upvalue()
   upvalue = nil
 end
end

But there are other ways to create upvalues. One more example:

local function foo(arg)
  return function()
    print(tostring(arg))
  end
end

my_function = foo({ --[[ something large ]] })

Alexander.