lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello all,

I have an odd problem with a weak table. It looks like this:


	weaktable = { __mode = "k" }
	setmetatable(weaktable, weaktable)

	-- Fill the table with some values
	weaktable[other_table] = true
	weaktable[second_table] = true
	...

	-- Later, I would like to traverse the table
	for key, value in pairs(weaktable) do
	  -- Do something that may trigger the garbage collector
	end


The problem is, if inside the for loop the garbage collector is
triggered, the garbage collector sometimes remove some entries in the
weaktable and the loop will stop before all remaining pairs are visited.
I think this is because during the loop it isn't save to modify the table.

I don't know a solution to prevent this problem. I didn't found a way to
lock the weak table in someway. Setting __mode to nil doesn't help.

Any ideas?


Michael Roth


-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBNw51SIrOxc3jOmoRAqBGAJ4hox9w+vowMxSDTEZXwjeio/blbgCeMwym
qeEoZ191nwU8ZZrQsP3iBaE=
=Sojb
-----END PGP SIGNATURE-----