Skip to content

Commit

Permalink
Add callback freeing
Browse files Browse the repository at this point in the history
  • Loading branch information
theVerySharpFlat committed Nov 20, 2022
1 parent 8a65518 commit 7424cf7
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions imgui-lwjgl3/src/main/java/imgui/glfw/ImGuiImplGlfw.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.lwjgl.glfw.GLFWScrollCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWWindowFocusCallback;
import org.lwjgl.glfw.Callbacks;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
Expand Down Expand Up @@ -317,12 +318,11 @@ public void accept(final String str) {
prevUserCallbackScroll = glfwSetScrollCallback(windowId, this::scrollCallback);
prevUserCallbackKey = glfwSetKeyCallback(windowId, this::keyCallback);
prevUserCallbackChar = glfwSetCharCallback(windowId, this::charCallback);
prevUserCallbackMonitor = glfwSetMonitorCallback(this::monitorCallback);
}

// Update monitors the first time (note: monitor callback are broken in GLFW 3.2 and earlier, see github.com/glfw/glfw/issues/784)
updateMonitors();
glfwSetMonitorCallback(this::monitorCallback);
prevUserCallbackMonitor = glfwSetMonitorCallback(this::monitorCallback);


// Our mouse update function expect PlatformHandle to be filled for the main viewport
final ImGuiViewport mainViewport = ImGui.getMainViewport();
Expand Down Expand Up @@ -373,14 +373,19 @@ public void newFrame() {
public void dispose() {
shutdownPlatformInterface();

if (callbacksInstalled) {
glfwSetWindowFocusCallback(windowPtr, prevUserCallbackWindowFocus);
glfwSetCursorEnterCallback(windowPtr, prevUserCallbackCursorEnter);
glfwSetMouseButtonCallback(windowPtr, prevUserCallbackMouseButton);
glfwSetScrollCallback(windowPtr, prevUserCallbackScroll);
glfwSetKeyCallback(windowPtr, prevUserCallbackKey);
glfwSetCharCallback(windowPtr, prevUserCallbackChar);
callbacksInstalled = false;
try {
if (callbacksInstalled) {
glfwSetWindowFocusCallback(windowPtr, prevUserCallbackWindowFocus).free();
glfwSetCursorEnterCallback(windowPtr, prevUserCallbackCursorEnter).free();
glfwSetMouseButtonCallback(windowPtr, prevUserCallbackMouseButton).free();
glfwSetScrollCallback(windowPtr, prevUserCallbackScroll).free();
glfwSetKeyCallback(windowPtr, prevUserCallbackKey).free();
glfwSetCharCallback(windowPtr, prevUserCallbackChar).free();
callbacksInstalled = false;
}
glfwSetMonitorCallback(prevUserCallbackMonitor).free();
} catch (NullPointerException ignored) {
// ignored
}

for (int i = 0; i < ImGuiMouseCursor.COUNT; i++) {
Expand Down Expand Up @@ -693,6 +698,7 @@ public void accept(final ImGuiViewport vp) {
}
}

Callbacks.glfwFreeCallbacks(data.window);
glfwDestroyWindow(data.window);
}

Expand Down

0 comments on commit 7424cf7

Please sign in to comment.