This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libgcj/16998] New: WeakHashMap iterator can throw NoSuchElementException unexpectedly
- From: "mckinlay at redhat dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 12 Aug 2004 02:26:45 -0000
- Subject: [Bug libgcj/16998] New: WeakHashMap iterator can throw NoSuchElementException unexpectedly
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
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