Deleting on application exit with AWTGLCanvas

Started by elvencode, September 07, 2011, 18:22:03

Previous topic - Next topic

elvencode

Hello,
i followed the DemoBox example for implementing a 3D window inside my Swing Application. Everything works but i would like to delete the data uploaded to OpenGL (VBOs, textures, framebuffers, etc) when closing the JFrame of the application. The problem is that at that time the rendering context is not valid anymore (it gives NullPointer exceptions deleting). The example doesn't try to destroy on exit so i've forced it to do so.
If i destroy things when the application is opened all works. Have you any ideas on what to do that or just forget the problem?  ;D

Thanks

CodeBunny

Call Display.destroy() when you detect a close event? It's pretty easy.

Also, if the rendering context is no longer valid, I would assume that it is automatically destroyed.

Fool Running

You shouldn't have to worry about destroying all of the OpenGL data during a program exit. The drivers clean up automatically when the OpenGL context gets destroyed (which happens when the application closes).
If, for some reason, you really want to do it, then your only option is to clean up when the context is still alive (i.e. before the window is closed).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Chuck

As I understand it, you only need Display.destroy() if you're planning on continuing to run afterward, e.g. if you have a launcher that launches different apps in-process then returns to the menu.  In that case, calling Display.destroy() in your JFrame's WindowListener.windowClosing event ought to do the trick.  If your app is exiting anyway, your OS will take care of it.

jediTofu

There have been known bugs where not calling AL.destroy() threw exceptions when closing the program, but have never seen any problems without calling Display.destroy() (but that doesn't mean there aren't or could be!).

As codebunny as already said, you can use a window listener:

myjframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
 // destroy stuff
 Display.destroy();
}
});

However, I'm betting that your game thread is on a different thread than the main one.  If you download the source zip of LWJGL (or look at the wiki page), the Applet and Window examples show an example of doing this by overriding #removeNotify() method on the Canvas.  [EDIT:  To be more specific:  It creates a loop in the #addNotify method, and then joins with the Thread to wait for it to die in the #removeNotify method.  At the end of the loop in #addNotify, it calls the appropriate #destroy()'s.]
cool story, bro