Skip to content

Commit

Permalink
Fix for SI-6706, Symbol breakage under GC.
Browse files Browse the repository at this point in the history
Ensure the map key and the String in the Symbol are the
same reference by removing the old key before updating the
map with the new key -> symbol relation.
  • Loading branch information
paulp committed Nov 24, 2012
1 parent 5b498a2 commit d0de367
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/library/scala/Symbol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ private[scala] abstract class UniquenessCache[K, V >: Null]
val res = cached()
if (res != null) res
else {
// If we don't remove the old String key from the map, we can
// wind up with one String as the key and a different String as
// as the name field in the Symbol, which can lead to surprising
// GC behavior and duplicate Symbols. See SI-6706.
map remove name
val sym = valueFromKey(name)
map.put(name, new WeakReference(sym))
sym
Expand Down
14 changes: 14 additions & 0 deletions test/files/run/t6706.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object Test {
var name = "foo" + 1
var s1 = Symbol(name)
s1 = null
System.gc
val s2 = Symbol("foo1")
name = null
System.gc
val s3 = Symbol("foo1")

def main(args: Array[String]): Unit = {
assert(s2 eq s3, ((s2, System.identityHashCode(s2), s3, System.identityHashCode(s3))))
}
}

0 comments on commit d0de367

Please sign in to comment.