This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libgcj/16998] New: WeakHashMap iterator can throw NoSuchElementException unexpectedly


The following test case demonstrates that WeakHashMap's iterator().next() can
throw NoSuchElementException even if hasNext() returned true. This probably
should not be possible - although WeakHashMap is a special case by the spec,
iterator behaviour is not one of those cases. 

It seems reasonable to expect that if hasNext() returns true, next() will not fail.

Possible solution: have hasNext() grab a reference to the element which will be
returned by the subsequent next() call.

import java.util.WeakHashMap;

public class WH {

  public static void main(String[] s) {
    WeakHashMap w = new WeakHashMap();
    for (int i = 0; i < 100000; i++)
      w.put(new WH(), null);

    for (int i = 0; i < 1000; i++)
      System.out.println(w.keySet().toArray().length);
  }
}

-- 
           Summary: WeakHashMap iterator can throw NoSuchElementException
                    unexpectedly
           Product: gcc
           Version: 3.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libgcj
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mckinlay at redhat dot com
                CC: gcc-bugs at gcc dot gnu dot org,java-prs at gcc dot gnu
                    dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16998


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]