fabiofai / jnibwapi

Automatically exported from code.google.com/p/jnibwapi
0 stars 0 forks source link

Use of JNIBWAPI.getAllUnits is liable to result in a ConcurrentModificationException #6

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The following code is liable to result in a ConcurrentModificationException 
(emanating from the "for" line):

for (Unit unit : bwapi.getAllUnits()) {
    logger.finest("Unit " + unit.getID() + ": Type ID = "
            + unit.getTypeID());
}

The reason is, I think, because JNIBWAPI.getAllUnits() just returns 
units.values(), and while my code is iterating through that, the 
JNIBWAPI.gameUpdate() method is likely to be modifying the units member 
variable.

The workaround is this:

for (Unit unit : bwapi.getAllUnits().toArray(new Unit[0])) {
    logger.finest("Unit " + unit.getID() + ": Type ID = "
            + unit.getTypeID());
}

Not a bug, but it would be nice if the returned Collection of 
JNIBWAPI.getAllUnits was threadsafe.

Original issue reported on code.google.com by jamesric...@googlemail.com on 24 Apr 2011 at 5:29

GoogleCodeExporter commented 8 years ago

Original comment by gdo...@gmail.com on 11 Jul 2011 at 9:53