Skip to content

Commit

Permalink
Clean up callbacks when disposing of window.
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcsmith-net committed May 16, 2023
1 parent 4e1e514 commit 116e409
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions processing-lwjgl/src/main/java/processing/lwjgl/PSurfaceLWJGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

}

}

0 comments on commit 116e409

Please sign in to comment.