WeakHashMap.keySet ().toArray () can throw NoSuchElementException

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P3
    • 1.4.0
    • Affects Version/s: 1.3.0
    • Component/s: core-libs
    • None

      The implementation of AbstractCollection.toArray () is as follows:

      public Object[] toArray (Object a[]) {
          int size = size ();
          ...
          Iterator it = iterator ();
          for (int i=0; i < size; i++)
              a [i] = it.next ();
          ...
      }

      In case of WeakHashMap.keySet () (it is a collection), a situation when GC clears some weak keys after the size is obtained can occure (even if we synchronize on the WeakHashMap). It implies that java.util.NoSuchElementException is thrown, because the 'for' cycle expects 'size' elements in the iterator 'it'.

            Assignee:
            Mark Reinhold
            Reporter:
            J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: