Scala Programming Language
  1. Scala Programming Language
  2. SI-6706

Symbols aren't always unique for equal strings

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.2, Scala 2.10.0-RC2
    • Fix Version/s: Scala 2.10.1
    • Component/s: Misc Library
    • Labels:
      None

      Description

      See the following REPL session:

      Welcome to Scala version 2.10.0-RC3 (OpenJDK 64-Bit Server VM, Java 1.7.0_09).
      Type in expressions to have them evaluated.
      Type :help for more information.
      
      scala> var name = "foo" + 1
      name: String = foo1
      
      scala> var s1 = Symbol(name)
      s1: Symbol = 'foo1
      
      scala> s1 = null
      s1: Symbol = null
      
      scala> System.gc
      
      scala> val s2 = Symbol("foo1")
      s2: Symbol = 'foo1
      
      scala> name = null
      name: String = null
      
      scala> System.gc
      
      scala> val s3 = Symbol("foo1")
      s3: Symbol = 'foo1
      
      scala> s2 eq s3
      res2: Boolean = false
      

      The problem arises if the symbol object for a particular name is garbage collected (so the weak reference stored in Symbol's weak hash map is null), but the string for that name has not been garbage collected (so the key still exists in the weak hash map). If a new symbol object with the same name is added, the weak hash map key will be a different object from the name field of the symbol object. If that string is subsequently garbage collected, the weak hash map entry disappears, so if the symbol is then created for a third time, it will end up being a different object from the second one.

      For additional background, see https://groups.google.com/d/topic/scala-user/mp_pSbfRECI/discussion

        Activity

        Show
        Paul Phillips added a comment - https://github.com/scala/scala/pull/1662
        Hide
        Paul Phillips added a comment -

        d0de367e72

        Show
        Paul Phillips added a comment - d0de367e72

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Paul Butcher
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development