Friday 30 October 2015

EXCEPTION_ACCESS_VIOLATION

While following the tutorial that I wrote about in my last entry I got stuck on the lesson about spawning items - my game randomly crashed the JVM when spawning a mushroom with a very nice fatal error:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000066bcbd0d, pid=156, tid=7088
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b15) (build 1.8.0_45-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [gdx-box2d64.dll+0xbd0d]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Today I finally had the time to dig into my code (since I don't use Brent's code 1:1, I like to "tidy it up" after each lesson so it adheres to my own standards) and managed to find the culprint.

The problem was in the end that PlayScreen.update() tried to set the boodies of destroyed Goombas active - that caused the Box2d engine to try to access memory that is not available anymore (Box2D is written in C, there is just a tiny Java-Wrapper around it so it can do that).

The solution is something like this:
In PlayScreen.update() instead of

if(enemy.getX() < player.getX() + 224 / MarioBros.PPM) {
                enemy.b2body.setActive(true);
            }

do something like

if(!enemy.isDestroyed() && enemy.getX() < player.getX() + 224 / MarioBros.PPM) {
                enemy.b2body.setActive(true);
            }

For that to work we need to create this variable and method in Enemy:

protected boolean destroyed;

public boolean isDestroyed() {
                return destroyed{
            }
and remove the variable "destroyed" from Goomba and Turtle.

My own current progress can be found here.