From 116e409fb0f8eaf09584fc9271f6c2e8012218a0 Mon Sep 17 00:00:00 2001 From: Neil C Smith Date: Tue, 16 May 2023 12:26:44 +0100 Subject: [PATCH] Clean up callbacks when disposing of window. --- .../java/processing/lwjgl/PSurfaceLWJGL.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/processing-lwjgl/src/main/java/processing/lwjgl/PSurfaceLWJGL.java b/processing-lwjgl/src/main/java/processing/lwjgl/PSurfaceLWJGL.java index 2088347..c5169d2 100644 --- a/processing-lwjgl/src/main/java/processing/lwjgl/PSurfaceLWJGL.java +++ b/processing-lwjgl/src/main/java/processing/lwjgl/PSurfaceLWJGL.java @@ -211,6 +211,10 @@ public void initFrame(PApplet sketch) { this.sketch = sketch; + if (initCount == 0) { + glfwSetErrorCallback(new ErrorHandler()); + } + if (!glfwInit()) { PGraphics.showException("Unable to initialize GLFW"); } @@ -219,13 +223,6 @@ public void initFrame(PApplet sketch) { System.out.println("GLFW initialized: " + glfwGetVersionString()); } - // GLFW error callback - addCallback(GLFW::glfwSetErrorCallback, GLFWErrorCallback - .create((error, description) -> { - String message = MemoryUtil.memUTF8(description); - PGraphics.showWarning("GLFW error " + error + ": " + message); - })); - // TODO initIcons(); initDisplay(); @@ -1205,11 +1202,17 @@ private void handleRun() { PApplet.mainThread().runLater(() -> { // Need to clean up before exiting + Callbacks.glfwFreeCallbacks(window); + callbacks.clear(); glfwDestroyWindow(window); glfwPollEvents(); initCount--; if (initCount <= 0) { glfwTerminate(); + GLFWErrorCallback err = glfwSetErrorCallback(null); + if (err != null) { + err.free(); + } initCount = 0; } sketch.exitActual(); @@ -1650,4 +1653,13 @@ public void dampenForLowResTicker() { } } + private static class ErrorHandler implements GLFWErrorCallbackI { + @Override + public void invoke(int error, long description) { + String message = MemoryUtil.memUTF8(description); + PGraphics.showWarning("GLFW error " + error + ": " + message); + } + + } + }