Page 1 of 1
ArrayList fastRemove exception
Posted: 20 Nov 2013, 05:45
by Zelek
I am getting an ArrayIndexOutOfBoundsException in my RequestHandler, and it's apparently happening in ArrayList.fastRemove(). This is the offending code in my extension:
Code: Select all
if (_playerHashMap.containsKey(location)) {
List<Entity> playerList = _playerHashMap.get(location);
if (playerList.contains(player)) {
playerList.remove(player); // <-- this is where the exception occurs
}
}
The exception is fairly rare, and it occurs even when I'm the only client connected to the server, so I'm not sure what the cause could be. Since the line right before verifies that the Player is in the List, I can only assume that two requests are being executed simultaneously. Does that seem plausible?
Re: ArrayList fastRemove exception
Posted: 20 Nov 2013, 08:25
by Lapo
Could you please report the full stack trace of the error?
The List.remove(...) method is not documented to throw an ArrayOutOfBoundException. What kind of list implementation are you using?
Re: ArrayList fastRemove exception
Posted: 20 Nov 2013, 09:20
by Zelek
Here's the stack trace:
Code: Select all
19 Nov 2013 | 20:26:41,421 | ERROR | com.smartfoxserver.v2.controllers.ExtensionController-3 | v2.controllers.ExtensionController | | java.lang.ArrayIndexOutOfBoundsException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: java.lang.ArrayIndexOutOfBoundsException
Message: -1
Description: Error while handling client request in extension: { Ext: GalaxiesExtension, Type: JAVA, Lev: ZONE, { Zone: GalaxiesZone }, {} }
Extension Cmd: MoveRequest
+--- --- ---+
Stack Trace:
+--- --- ---+
java.util.ArrayList.fastRemove(Unknown Source)
java.util.ArrayList.remove(Unknown Source)
com.codecommode.Managers.EntityManager.updateActiveEnt(EntityManager.java:123)
com.codecommode.Entity.Entity.updateLocation(Entity.java:249)
com.codecommode.Managers.MoveManager.handleMove(MoveManager.java:42)
com.codecommode.Requests.MoveRequest.handleInternalRequest(MoveRequest.java:84)
com.codecommode.Requests.ExclusiveRequestHandler.handleClientRequest(ExclusiveRequestHandler.java:25)
com.codecommode.Requests.MoveRequest.handleClientRequest(MoveRequest.java:31)
com.smartfoxserver.v2.extensions.SFSExtension.handleClientRequest(SFSExtension.java:192)
com.smartfoxserver.v2.controllers.ExtensionController.processRequest(ExtensionController.java:133)
com.smartfoxserver.bitswarm.controllers.AbstractController.run(AbstractController.java:96)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
The code that I showed in my original post was line 123 of EntityManager.java. I am using an ArrayList.
Re: ArrayList fastRemove exception
Posted: 20 Nov 2013, 09:41
by Lapo
Thanks,
it's a concurrency issue with the ArrayList. If two threads are running the same remove() operation "simultaneously" the problem will arise. In general all Java standard List implementations are not thread safe.
Two basic possible solutions:
1- synchronize manually
2- User Collections.synchronizedList(...) (
see Oracle javadoc )
cheers