'EXCEPTION_ACCESS_VIOLATION Error when using stbi_load
I'm trying to load an image into a ByteBuffer using stbi_load
. However, the following error gets thrown up in the console
fatal error has been detected by the Java Runtime Environment: EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffb5bb63b8c, pid=5424, tid=0x0000000000001364 JRE version: OpenJDK Runtime Environment (8.0_312-b07) (build 1.8.0_312-b07) Java VM: OpenJDK 64-Bit Server VM (25.312-b07 mixed mode windows-amd64 compressed oops) Problematic frame: C [lwjgl_stb.dll+0x3b8c] Failed to write core dump. Minidumps are not enabled by default on client versions of Windows An error report file with more information is saved as: C:\Users\nottn\eclipse-workspace\Bon Sea\hs_err_pid5424.log If you would like to submit a bug report, please visit: https://bugzilla.redhat.com/enter_bug.cgi The crash happened outside the Java Virtual Machine in native code. See problematic frame for where to report the bug.
Here's the code that produces this error (keep in mind I'm using LWJGL 3 by th)
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.stb.STBImage.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.awt.image.BufferedImage;
public class Texture {
private int id;
private IntBuffer width, height, n;
public Texture(String filename)
{
BufferedImage bi;
try
{
bi = ImageIO.read(new File("./sprites/" + filename));
width = IntBuffer.allocate(bi.getWidth());
width.put(bi.getWidth());
height = IntBuffer.allocate(bi.getHeight());
height.put(bi.getHeight());
n = IntBuffer.allocate(GL_RGBA);
n.put(GL_RGBA);
ByteBuffer data = stbi_load("/Users/nottn/eclipse-workspace/Bon Sea/sprites/test.png", width, height, n, STBI_rgb_alpha);
id = glGenTextures();
glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width.array()[0], height.array()[0], 0, GL_RGBA, GL_BYTE, data);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void Bind() {
glBindTexture(GL_TEXTURE_2D, id);
}
}
And this is the log file that gets generated Pastebin Log Here
Any answers as to why this error is occurring are greatly appreciated.
Solution 1:[1]
The cause of the crash is that you are using non-direct NIO buffers backed by Java arrays, i.e. IntBuffer.allocate(...)
instead of ByteBuffer.allocateDirect(...).asIntBuffer()
which are backed by off-heap (native) virtual memory.
LWJGL is basically just forwarding/directing the stbi_load(...)
call to the native library. And for the native library to be able to put the memory into a supplied memory region, that region must be an actual "off-heap" (off-heap as in: it is outside the Java garbage-collected heap) memory region of the process'es virtual memory.
When you call any LWJGL-provided method taking a NIO Buffer (such as stbi_load(...)
) what LWJGL will do is to read the value of the (hidden) Buffer.address
field, which is only filled when said buffer is a direct (or "off-heap") buffer.
In the case of an non-direct or (on-heap) buffer, which are backed by Java primitive arrays, that field is 0
, and therefore the native library will access the zero address, which ultimately causes the crash.
In order to fix this, you should therefore create direct NIO buffers only.
For an explanation of what a non-direct vs. a direct NIO buffer is, see the JavaDocs of the ByteBuffer class.
I also highly recommend reading this LWJGL 3 blog article, which provides more insight and more strategies around native-memory management in LWJGL 3.
Solution 2:[2]
Not shure if that's the anwser to your problem, but I had that error when I accidentally move similar code to execute BEFORE line "GL.createCapabilities();"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Guczi |