I'm using LWJGL MemoryStack together with JOML like this:
(ns learnopengl.transformations
(:import [org.joml Vector3f Matrix4f]
[org.lwjgl.system MemoryStack]))
(def memory-stack (MemoryStack/stackPush))
(defn translate-n-rotate
[mat4 angle]
(.rotation mat4 (float angle) (new Vector3f (float 0) (float 0) (float 1)))
(.translate mat4 (new Vector3f (float 0.5) (float -0.5) (float 0)))
(.get mat4 (.mallocFloat memory-stack 16)))
This is Clojure; in Java this should look something like this:
class Piep {
MemoryStack memory-stack = MemoryStack.stackPush();
public FloatBuffer translate-n-rotate(Matrix4f mat4, float angle) {
mat4.rotation(angle, new Vector3f(0f, 0f, 1f));
mat4.translate(new Vector3f(0.5f, -0.5f, 0f));
return mat4.get(memory-stack.mallocFloat(16));
}
}
The memory error occurs after running the program a few seconds. I can only assume that I'm not properly releasing some memory after it's no longer needed.
I don't want to increase the memory of MemoryStack and neither the memory of the jvm, because that would only postpone the problem. I want to learn how to use MemoryStack properly. Unfortunately, the documentation for MemoryStack is rather thin.
Code reproducing the error can be found here (Clojure).
From the documentation of the push
method:
This method should be called when entering a method, before doing any stack allocations. When exiting a method, call the pop() method to restore the previous stack frame.
Your code never calls pop
, or stackPop
. It has to be called somewhere, when you don't need that particular stack frame anymore.
Just as a suggestion on how to figure out proper usages of something based on other people's code - you can use grep.app
. For example, https://grep.app/search?f.lang=Java&case=true&words=true&q=MemoryStack