Skip to content

Commit ddb4643

Browse files
Lunafinaclaude
andcommitted
fix(lwjgl3): switch GLFWGamepadState memory strategy to fix double free
* GLFWGamepadState creation with `.create()` creates a java native ByteBuffer backing the GLFWGamepadState. The GLFWGamepadState's `.close()` uses the NativeResource `.close()` default implementation, that calls `.free()`. Which, at some point, causes a crash from trying to free a java natively allocated object. That manifests as a double free. * As a fix, either using `.create()` with no `.close()`, letting the JVM handle the gamepad state, or using `.malloc()` with `.close()` or `.free()`, handling the cpp native memory manually. Co-authored-by: Claude <noreply@anthropic.com>
1 parent d95baae commit ddb4643

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

‎imgui-lwjgl3/src/main/java/imgui/glfw/ImGuiImplGlfw.java‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,17 +1143,16 @@ protected void updateGamepads() {
11431143
final MapAnalog mapAnalog;
11441144

11451145
if (glfwHasGamepadApi) {
1146-
try (GLFWGamepadState gamepad = GLFWGamepadState.create()) {
1147-
if (!glfwGetGamepadState(GLFW_JOYSTICK_1, gamepad)) {
1148-
return;
1149-
}
1150-
mapButton = (keyNo, buttonNo, _unused) -> io.addKeyEvent(keyNo, gamepad.buttons(buttonNo) != 0);
1151-
mapAnalog = (keyNo, axisNo, _unused, v0, v1) -> {
1152-
float v = gamepad.axes(axisNo);
1153-
v = (v - v0) / (v1 - v0);
1154-
io.addKeyAnalogEvent(keyNo, v > 0.10f, saturate(v));
1155-
};
1146+
GLFWGamepadState gamepad = GLFWGamepadState.create();
1147+
if (!glfwGetGamepadState(GLFW_JOYSTICK_1, gamepad)) {
1148+
return;
11561149
}
1150+
mapButton = (keyNo, buttonNo, _unused) -> io.addKeyEvent(keyNo, gamepad.buttons(buttonNo) != 0);
1151+
mapAnalog = (keyNo, axisNo, _unused, v0, v1) -> {
1152+
float v = gamepad.axes(axisNo);
1153+
v = (v - v0) / (v1 - v0);
1154+
io.addKeyAnalogEvent(keyNo, v > 0.10f, saturate(v));
1155+
};
11571156
} else {
11581157
final FloatBuffer axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1);
11591158
final ByteBuffer buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1);

0 commit comments

Comments
 (0)