Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LWJGL-OpenGL上下文在关闭钩子中丢失_Java_Opengl_Lwjgl_Shutdown Hook - Fatal编程技术网

Java LWJGL-OpenGL上下文在关闭钩子中丢失

Java LWJGL-OpenGL上下文在关闭钩子中丢失,java,opengl,lwjgl,shutdown-hook,Java,Opengl,Lwjgl,Shutdown Hook,我目前正在使用Java和LWJGL3,我正在为顶点数组对象、顶点缓冲区对象等编写一些包装器。 现在,在程序退出之前删除这些对象是一个好习惯,所以我创建了一个shutdownhook来进行清理 但是,当我在shutdown钩子中调用OpenGL函数时,我得到一个非法状态异常,它表示OpenGL上下文尚未初始化 我编写了一个测试程序,再现了这种行为: public static void main(String[] args) { GLFW.glfwInit(); long wind

我目前正在使用Java和LWJGL3,我正在为顶点数组对象、顶点缓冲区对象等编写一些包装器。 现在,在程序退出之前删除这些对象是一个好习惯,所以我创建了一个shutdownhook来进行清理

但是,当我在shutdown钩子中调用OpenGL函数时,我得到一个非法状态异常,它表示OpenGL上下文尚未初始化

我编写了一个测试程序,再现了这种行为:

public static void main(String[] args) {
    GLFW.glfwInit();
    long window = GLFW.glfwCreateWindow(100, 100, "", 0, 0);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            GL15.glDeleteBuffers(0);
            GLFW.glfwTerminate();
        }
    });

    while (!GLFW.glfwWindowShouldClose(window)) {
        GLFW.glfwPollEvents();
    }
}
堆栈跟踪:

Exception in thread "Thread-0" java.lang.IllegalStateException: No GLCapabilities instance set for the current thread. Possible solutions:
a) Call GL.createCapabilities() after making a context current in the current thread.
b) Call GL.setCapabilities() if a GLCapabilities instance already exists for the current context.
at org.lwjgl.opengl.GL.getCapabilities(GL.java:241)
at org.lwjgl.opengl.GL15.nglDeleteBuffers(GL15.java:152)
at org.lwjgl.opengl.GL15.glDeleteBuffers(GL15.java:178)
at core.Main$1.run(Main.java:11)
有人知道为什么上下文会自动被破坏吗


如果您需要任何额外的信息,请说出来。

OpenGL上下文始终与一个线程(或没有线程)关联。只能从上下文绑定到的线程对特定上下文调用函数


由于关闭挂钩启动了一个新线程,因此在发出任何命令之前,必须将OpenGL上下文绑定到该线程。

关闭挂钩的目的是什么?
glfwWindowShouldClose
不够好吗?请注意,OpenGL上下文绑定到一个特定线程。因为您正在生成一个新线程,所以在使用之前必须绑定上下文。@Nicolas否,我想将此接口与其他类解耦,所以我不想调用Dispeall方法或其他东西。@RagingRabbit:我没有看到任何“解耦”,因为销毁仍然在
main
中定义。不管关闭钩子将要做什么,都可以像循环完成时
main
调用的函数一样有效地完成。在什么方面还没有充分“解耦”?@BDL感谢您指出这一点!你能把这个作为回答吗?