Debug display problem

Moderator: Moderator

Post Reply
Message
Author
aardvark
Wyrmic
Posts: 200
Joined: Wed Aug 22, 2012 12:16 am

Debug display problem

#1 Post by aardvark »

While looking at the contents of tables in debug mode, I noticed that a field called "__orderedIndex" would sometimes appear where there should be none. It appears on every other inspection, in every inspected table.

I traced the problem to the table.orderedPairs() function in utils.lua. In addition to not correctly cleaning up after itself, it clobbers the global variable key and uses O(n^2) comparisons against the sorted index table (the previously mentioned "__orderedIndex" field) over the course of the full table iteration.

Here's a version that is shorter, easier to read (I think), and may run faster without touching any globals or leaving remnants that could cause problems. This one function replaces __genOrderedIndex(), orderedNext(), and table.orderedPairs() in utils.lua (lines 291-330).

Code: Select all

function table.orderedPairs(t)
	local sorted_keys = {}
	local i = 0		-- iterator index
	local n = 0		-- size of key table
	for key, _ in pairs(t) do
		n = n + 1
		sorted_keys[n] = key
	end
	table.sort(sorted_keys, cmp_multitype)

	return function ()
		i = i + 1
		if i <= n then
			local k = sorted_keys[i]
			return k, t[k]
		end
	end
end

Hachem_Muche
Uruivellas
Posts: 744
Joined: Thu Nov 18, 2010 6:42 pm

Re: Debug display problem

#2 Post by Hachem_Muche »

I've noticed this problem also, and it occasionally causes problems with spurious fake bugs.

Any chance it's related to http://forums.te4.org/viewtopic.php?f=44&t=34969 ?
Author of the Infinite 500 and PlenumTooltip addons, and the joys of Scaling in ToME.

aardvark
Wyrmic
Posts: 200
Joined: Wed Aug 22, 2012 12:16 am

Re: Debug display problem

#3 Post by aardvark »

I'd say so. If you inspected it from the debug console (=game.player.tmp), it would leave behind a remnant subtable with "__orderedIndex" as the key. PlayerDisplay.lua and Minimalist.lua both proceed to use that key to access a nil reference (lines 424-427 and 1343-1346 from v1.0, respectively):

Code: Select all

for eff_id, p in pairs(player.tmp) do
	local e = player.tempeffect_def[eff_id]
	if e.status == "detrimental" then bad_e[eff_id] = p else good_e[eff_id] = p end
end
eff_id takes the value "__orderedIndex" but player.tempeffect_def["__orderedIndex"] is non-existent, so e becomes nil. Then an attempt is made to access a field in not-a-table e. And there's your error message.

Post Reply