Repository navigation
Conversation
* 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>
Lunafina
force-pushed
the
fix/gamepad_state_invalid_memory_management
branch
from
October 2, 2026 13:25
6d24ddf to
ddb4643
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
.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..create()with no.close(), letting the JVM handle the gamepad state, or using.malloc()with.close()or.free(), handling the cpp native memory manually.Summary
Replace the try-with-resource statement handling GLFWGamepadState in ImGuiImplGlfw causing a memory related crash after a certain time. The crash can be reproduced by using GLFW + OpenGL (3.3+), and setting the config flag
NavEnableGamepadand using repeatedly the ImGuiImplGlfw.newFrame()method:Type of change
Notes for reviewer
The memory related crash seems to come from the fact that the gamepad object storing the gamepad state is handled by a try-with-resource statement automatically calling the
.close()method of the object. This method is using the default implementation of theNativeResourceinterface, which calls.free(), releasing the allocator allocated address. But, thegamepadis actually created using.create()which uses java native object creation. Thus, the.close()is trying to free a java allocated address, causing the crash.There are two possible fixes, either gamepad can be created with
.malloc(), the memory would then be allocated and freed the C++ way, or the try-with-resource statement can be removed to simply use the java native object the java way, letting the JVM clean the memory.I opted to fix this issue by staying in Java, thus removing the try-with-resource statement.
In the investigation process of this memory related crash, having only
double free in tcache2as clue, I used Claude Haiku 4.5 when I was out of idea. It redirected me to look at the other part of the code, such as the gamepad code. Thus, it helped me to locate the bug but did not write any code. So I don't know if it should be acknowledged as co-author. Let me know if it should not.