lua-users home
lua-l archive

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


What if the value is not a table? 
---
tab = {}
setmetatable(tab, {__mode = 'v'})

r = function() end
tab['key'] = r

r = nil
collectgarbage()

When ‘r' is collected, how could ‘tab’ know that?

Thanks.
/hz

在 2018年9月3日,上午8:32,Oliver Kroth <oliver.kroth@nec-i.de> 写道:



Am 03.09.2018 um 17:18 schrieb Zhao Han:
Hi, All,

For a weak table, is it possible to use some meta table tricks to track the changes of the table?

e.g. when a key or value get collected, print a message.

Best Regards,
/hz
You could try to use the __gc field at a metatable at the objects that are going to get collected

--
Oliver