Application using JOGL stays in Limbo when closing
- by Roy T.
I'm writing a game using Java and OpenGL using the JOGL bindings. I noticed that my game doesn't terminate properly when closing the window even though I've set the closing operation of the JFrame to EXIT_ON_CLOSE. I couldn't track down where the problem was so I've made a small reproduction case. Note that on some computers the program terminates normally when closing the window but on other computers (notably my own) something in the JVM keeps lingering, this causes the JFrame to never be disposed and the application to never exit. I haven't found something in common between the computers that had difficulty terminating. All computers had Windows 7, Java 7 and the same version of JOGL and some terminated normally while others had this problem.
The test case is as follows:
public class App extends JFrame implements GLEventListener
{
private GLCanvas canvas;
@Override
public void display(GLAutoDrawable drawable)
{
GL3 gl = drawable.getGL().getGL3();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClear(GL3.GL_COLOR_BUFFER_BIT);
gl.glFlush();
}
// The overrides for dispose (the OpenGL one), init and reshape are empty
public App(String title, boolean full_screen, int width, int height)
{
//snipped setting the width and height of the JFRAME
GLProfile profile = GLProfile.get(GLProfile.GL3);
GLCapabilities capabilities = new GLCapabilities(profile);
canvas = new GLCanvas(capabilities);
canvas.addGLEventListener(this);
canvas.setSize(getWidth(), getHeight());
add(canvas);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //!!!
setVisible(true);
}
@Override
public void dispose()
{
System.out.println("HELP"); //
}
public static void main( String[] args )
{
new App("gltut 01", false, 1280, 720);
}
}
As you can see this doesn't do much more than adding a GLCanvas to the frame and registering the main class as the GLEventListener.
So what keeps lingering? I'm not sure. I've made some screenshots.
The application running normally.
The application after the JFrame is closed, note that the JVM still hasn't exited or printed a return code.
The application after it was force closed.
Note the return code -1, so it wasnt just the JVM standing by or something the application really hadn't exited yet.
So what is keeping the application in Limbo? Might it be the circular reference between the GLCanvas and the JFrame? I thought the GC could figure that out. If so how should I deal with that when I want to exit? Is there any other clean-up required when using JOGL? I've tried searching but it doesn't seem to be necessary.
Edit, to clarify: there are 2 dispose functions dispose(GLAutoDrawable arg) which is a member of GLEventListener and dispose() which is a member of JFrame. The first one is called correctly (but I wouldn't know what to there, destroying the GLAutoDrawable or the GLCanvas gives an infinite exception loop) the second one is never called.