Advertisement

Games crashes sometimes during restart

Started by January 07, 2016 11:35 AM
5 comments, last by Nanoha 8 years, 11 months ago

Hello gamedev community, I need some help plz. This ConcurrentModificationException-error is making me crazy! I just recently started android game development and programming in general.

When i play my game its flawless but sometims if im trying to start a new game it crashes. Logcat show these two lines in my code but i dont know how to fix it because they look correct to me :/

public void run() {
while (playing) {
update();
draw(); //the draw-method is marked
control(); } }

and in my draw-method:

// I draw the SpaceDust from an ArrayList
paint.setColor(Color.argb(255, 255, 255, 255));
for (SpaceDust sd : dustList) {
canvas.drawPoint(sd.getX(), sd.getY(), paint); }

Logcat output:

FATAL EXCEPTION: Thread-146798
Process: niclas.spacegame, PID: 7983
java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
at niclas.spacegame.TDView.draw(TDView.java:261)
at niclas.spacegame.TDView.run(TDView.java:174)
at java.lang.Thread.run(Thread.java:818)

Any ideas? Thanks Niclas

Does it crash every time? When you say 'sometimes' and along with that exception it looks like a race condition. You need to check what objects are being accessed from where and when. Not helpful I know but it sounds like something is being modified from two places at once. A good place to look might be where input is handled.

I'd say it's something to do with your spacedust. Maybe the array is being modified while you are drawing the dust. Check other places where dustList is modified and make sure they aren't done so at the same time. It would probably be useful to see your new game code too. It could be input comes in from one thread where you then start the new game but in another thread it is running your loop which is drawing the space dust.

Are you able to debug this? Can you set a break point and step through around the time this occurs or is Logcat all the feedback you get?

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Advertisement

What's very likely happening is you're modifying your dustList when you're starting or restarting your game. Your draw function is iterating over your drawList, so if your restart functionality changes the list you'll get that exception.

You'll need to make sure you avoid doing that either with a semaphore or flag. I'm not sure what the syntax is for Java but look up locking and thread safe if you're wanting to go the semaphore route. Or you can have a simple needsToRestart boolean, and when they click the restart button set that to true. Then your loop could look something like:


while (playing)
{
   if(needsToRestart)
        restart(); // Put your restart functionality in here.
    update();
    draw(); //the draw-method is marked
    control(); 
}

This way, your list isn't getting modified while you're trying to iterate over it.

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG


public void run() {
while (playing) {
update();
draw(); //the draw-method is marked
control(); } }

This is how your code actually looks?

I'm assuming this infinite loop is in some thread then?

Your problem is most likely that you try to modify your arraylist from another thread when starting a new game.

You are not allowed to do that, that will generate ConcurrentModificationException at "random" (statistics of how likely depends on the rest of the program)

ConcurrentModificationException is raised whenever an array is modified while you iterate over it.

@ Nanoha: If i finish a game the gamescreen stays the same. Then when I "tap to replay" it crashes. Sometimes i can play 5-10 games with no error and sometimes it crashes :/

I only have logcat to get feedback.

Check where you are modyfing the array (Initialization, inseration, delete, etc...)

A common solution is just to lock the array and copy it to another one, thus iterating over the array doesn't cause issues.

Advertisement

Eck's solution should work and should be quite simple to implement.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

This topic is closed to new replies.

Advertisement