Access violation when using stbi_load()

Started by colonisemars, September 02, 2016, 11:28:06

Previous topic - Next topic

colonisemars

Hi there! After a long night of trying to figure this out I've decided to ask for help here.

I am in the process of learning LWJGL, and to load an image, almost all sites and guides told me to use the stb library. So I did. And I'm getting an access violation. Here is my code

IntBuffer x,y,n;
x = IntBuffer.allocate(1);
y = IntBuffer.allocate(1);
n = IntBuffer.allocate(1);
ByteBuffer testooo;

try{
    testooo = stbi_load("./resources/Hammer.png", x, y, n, 0);
    System.out.println(stbi_failure_reason());
}catch (Exception e) {
    e.printStackTrace();
}


I have a folder "resources" in my buildpath, and it contains the image "Hammer.png".

(The class "Texturetest.java" is not used in the program)

If I change the image path to a non-existant file or location, it does not give an error and stbi_failure_reason() simply says "Unable to open file".

Here is a link to the pastebin containing the error and the log that was created for the error: http://pastebin.com/Ui5AC4v7

Thank you in advance.

CoDi

IntBuffer.allocate() allocates on the Java heap. For those buffer parameters, try using BufferUtils.createIntBuffer(), MemoryUtil.memAllocInt()/MemoryUtil.memFree(), or the MemoryStack functions instead.

try (MemoryStack stack = MemoryStack.stackPush()) {
			IntBuffer x = stack.mallocInt(1);
			// ...
		}

colonisemars

Quote from: CoDi on September 02, 2016, 12:08:03
IntBuffer.allocate() allocates on the Java heap. For those buffer parameters, try using BufferUtils.createIntBuffer(), MemoryUtil.memAllocInt()/MemoryUtil.memFree(), or the MemoryStack functions instead.

try (MemoryStack stack = MemoryStack.stackPush()) {
			IntBuffer x = stack.mallocInt(1);
			// ...
		}


Thank you so much! You're an absolute angel. I did not know the library did not have access to the java heap. Its working now!

abcdef

there are 2 ways to allocate memory for Byte Buffers, one to use the java heap memory and one to use off heap memory. The off heap memory is shareable so writing to the off heap memory allows you to pass data from java to a c program via JNI. The allocateDirect methods of ByteBuffer allow you allocate off heap and this is what the util class BufferUtils does under the scene.