使用 JOGL、顶点缓冲区对象并尝试在 Finalize 方法中释放 vbo 时,JVM 崩溃
我有一些自己编写的 CAD 软件。正在绘制的每个组件都有一组顶点缓冲区对象。如果组件被删除,我必须在 Finalize 方法中释放顶点缓冲区对象,例如:
if (gl != null) {
Integer[] keys = vbos.keySet().toArray(new Integer[0]);
for (int i = 0; i < keys.length; i++) {
Integer tmp = keys[i];
if (tmp != null) {
if (gl.glIsBufferARB(tmp.intValue()));
gl.glDeleteBuffersARB(1, new int[]{tmp.intValue()}, 0);
}
}
}
但是有时会出现 SIGSEV 和 JVM 崩溃。 hs_err 日志文件指向 gl.glIsBufferArb(tmp.intValue())。
我相信这意味着我的 gl 对象不再有效?
我认为它应该仍然有效。该程序仍在运行直到崩溃。有没有办法在不存储对 GL 对象的引用的情况下释放 glbuffer?
谢谢你!
I have some CAD software I've written. Each component being drawn has a set of vertex buffer objects. If the component gets deleted, I have to free the vertex buffer objects in the finalize method such as:
if (gl != null) {
Integer[] keys = vbos.keySet().toArray(new Integer[0]);
for (int i = 0; i < keys.length; i++) {
Integer tmp = keys[i];
if (tmp != null) {
if (gl.glIsBufferARB(tmp.intValue()));
gl.glDeleteBuffersARB(1, new int[]{tmp.intValue()}, 0);
}
}
}
however I sometimes get a SIGSEV and JVM crash. The hs_err log file points to gl.glIsBufferArb(tmp.intValue()).
I believe this means my gl object is no longer valid?
It should have been still valid I think. The program was still operating up until the crash. Is there a way to free a glbuffer without having stored a reference to the GL object?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将在这里遇到的最明显的问题是,OpenGL 上下文只能在其处于活动状态的线程中引用。一般来说,这将是您的渲染线程,这意味着 OpenGL 上下文在该线程中不可用。 JVM 的终结器线程。对我来说,这似乎是导致你错误的最可能原因。我建议您保留无效 VBO 的全局列表,并让 Finalize 方法将 id 添加到该列表中。然后,您可以从渲染线程定期处理该列表,从而使 OpenGL 调用成为实际删除 VBO 所必需的。
您可以在此处找到多线程环境中 OpenGL 行为的快速概述。
The most obvious issue you're going to run into here is that an OpenGL context can only be referenced in the thread it is active in. Generally speaking this will be your rendering thread, which means that the OpenGL context will not be available in the JVM's finalizer thread. That, to me, seems the most likely cause of your errors. I would recommend that you keep a global list of invalidated VBOs, and have the finalize method add ids to that list. You can then process that list periodically from your rendering threads, making the OpenGL calls necessary to actually delete the VBOs.
You can find a quick rundown on OpenGL's behavior in a multi-threaded environment here.