Actions
Bug #19531
closedObjectSpace::WeakMap: replaced values still clear the key they were assigned to
Description
Reproduction script¶
wmap = ObjectSpace::WeakMap.new
a = "A"
b = "B"
wmap[1] = a
wmap[1] = b # the table entry with 1 is still in the list of entries to clear when `a` is GCed
a = nil
GC.start
p wmap[1] # Should be `"B"`, but is `nil`
Explanation¶
What happens is that when we set wmap[1] = "A"
, WeakMap internally keeps a list of keys to clear when "A"
is GCed, e.g. pseudo code:
class WeakMap
def []=(key, value)
@hash[key] = value
@reverse[value] << key
end
end
But it doesn't clear previously kept mapping when a key is overwritten.
I'll work on a fix.
References¶
Actions
Like0
Like0Like0Like0