Reputation: 61
I am currently developing a game using LWJGL 3.0.0b and a strange occurrence has come up.
Background:
I have a custom entity class and an entity manager to store my entities in. When an entity is updated a method gets called named "tickTrail
" which adds in a trail entity to make the graphics visually better and entertaining.
Already I have simplified it down to the entities.size()
function, yes I try to solve this myself :P
A solution I have currently is replacing all methods similar to this:
for (int i = 0; i < entities.size(); i++)
{
//calls a method for drawing, updating, or input
}
to replace every single entities.size()
to size
. size
being a variable that changed upon when an entity is added, removed, list cleared, or only enemies removed.
This method DOES work 100%, window dragging does NOT crash the program using this method.
Question
So to sum up, the final question is why does this occur? I haven't determined why dragging a window and using the ArrayList's size() method crashes the program, yet storing a variable in its place is perfectly fine.
Any help or explanation to this strange phenomenon is highly appreciated :)
EDIT Last edit proved incorrect, even with the size variable in use the program still crashes, time to go back to the drawing board....
EDIT TWO Okay, so sometimes the window dragging works other times it doesn't, this might be more than just a simple size() method call :/
Upvotes: 0
Views: 256
Reputation: 1413
Based on what little you've provided, I'm guessing that multiple threads are accessing the ArrayList concurrently and the threads are stomping on all other.
If you're not creating a list via Collections.synchronizedList() to guarantee that all operations on the list are atomic so that the internals of the array aren't corrupted.
The reason I went down this path is that dragging Windows is likely generating multiple events to update the array, and you're trying to iterate through it at the same time.
When you use a variable to watch the size, then the concurrency issues go away. Of course, you likely don't have the correct values in the variable unless you are using AtomicInteger or whatever, but at least the exceptions aren't being thrown.
Upvotes: 2