@Override
    protected void destroyContext() {
        try {
            // invalidate the state so renderer can resume operation
            if (!USE_SHARED_CONTEXT) {
                renderer.cleanup();
            }

            if (Display.isCreated()) {
                /*
                 * FIXES: org.lwjgl.LWJGLException: X Error BadWindow (invalid Window parameter)
                 * request_code: 2 minor_code: 0
                 *
                 * Destroying keyboard early prevents the error above, triggered by destroying keyboard in
                 * by Display.destroy() or Display.setParent(null). Therefore, Keyboard.destroy() should
                 * precede any of these calls.
                 */
                if (Keyboard.isCreated()) {
                    // Should only happen if called in
                    // LwjglAbstractDisplay.deinitInThread().
                    Keyboard.destroy();
                }

                // try {
                // NOTE: On Windows XP, not calling setParent(null)
                // freezes the application.
                // On Mac it freezes the application.
                // On Linux it fixes a crash with X Window System.
                if (
                    JmeSystem.getPlatform() == Platform.Windows32 ||
                    JmeSystem.getPlatform() == Platform.Windows64
                ) {
                    // Display.setParent(null);
                }
                // } catch (LWJGLException ex) {
                // logger.log(Level.SEVERE, "Encountered exception when setting parent to null", ex);
                // }

                Display.destroy();
            }

            // The canvas is no longer visible,
            // but the context thread is still running.
            if (!needClose.get()) {
                // MUST make sure there's still a context current here.
                // Display is dead, make PBuffer available to the system.
                makePbufferAvailable();

                renderer.invalidateState();
            } else {
                // The context thread is no longer running.
                // Destroy pbuffer.
                destroyPbuffer();
            }
        } catch (LWJGLException ex) {
            listener.handleError("Failed make pbuffer available", ex);
        }
    }
This is called: 1) When the context thread ends 2) Any time the canvas becomes non-displayable
jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java