Effective

Effective

5M Downloads

Caught Errors in `enhanced 'for'` Loop

lonefelidae16 opened this issue ยท 0 comments

commented

Hi๐Ÿ‘‹
I got some errors at WaterfallCloudGenerators.tickParticles(WaterfallCloudGenerators.java:67).

java.lang.ArrayIndexOutOfBoundsException:
crash-2022-04-27_17.51.03-client.txt

java.lang.NullPointerException:
crash-2022-04-27_21.40.56-client.txt

I feel that the crash rate is particularly high when moving chunks around. So I tried these test codes:

public class Main {
    public static final Object2IntMap<BlockPos> particlesToSpawn = new Object2IntOpenHashMap<>();

    public static void main(String[] args) throws InterruptedException {
        particlesToSpawn.put(new BlockPos(0, 0, 8), 3);
        particlesToSpawn.put(new BlockPos(0, 0, 1), 0);
        particlesToSpawn.put(new BlockPos(0, 0, 4), 0);
        particlesToSpawn.put(new BlockPos(0, 0, 0), 2);

        Thread updater1 = new Thread(Main::updateParticles);
        Thread updater2 = new Thread(Main::updateParticles);
        updater1.start();
        updater2.start();
        updater1.join();
        updater2.join();
    }

    public static void updateParticles() {
        for (BlockPos pos : particlesToSpawn.keySet()) {
            if (pos != null) {
                if (particlesToSpawn.put(pos, particlesToSpawn.getInt(pos) - 1) <= 0) {
                    particlesToSpawn.removeInt(pos);
                }
                System.out.printf("%s, particlesToSpawn.size() == %d%n", pos, particlesToSpawn.size());
            }
        }
    }
}

... and I got java.lang.NullPointerException.

Perhaps this crash seems to be caused by deletion in the enhanced 'for' loop.

By enclosing this with synchronized and replacing ObjectSet<Object2IntMap.Entry<K>>#removeIf(Predicate), I have not had any problems so far.

Thanks for awesome mod!