-
-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Native functions may cause a crash if GC happens #1034
Comments
The question is whether the caller should ensure that the buffer or whatever resource remains reachable during a native call or LWJGL should be taking care of this. (with |
Hey @xxDark, This is a legitimate concern and something I've been worrying about since the very first lwjgl3 commit. For reference, the issue is explained in detail here: JVM Anatomy Quark #8: Local Variable Reachability In the context of LWJGL, the problem may occur in code such as: // in user code
ByteBuffer buffer = ByteBuffer.allocateDirect(...);
bindingMethod(..., buffer, ...);
// in LWJGL code
void bindingMethod(..., ByteBuffer buffer, ...) {
long a = memAddress(buffer);
nbindingMethod(..., a, ...);
} LWJGL is being naughty here, because it reads the buffer's off-heap memory address, then proceeds to call the native method, which accepts the buffer address, not the buffer object itself. When this code gets JIT compiled (and everything gets inlined nicely), the buffer instance is considered unreachable after the I've never been able to reproduce this and there have been no relevant bug reports in the 10 years since lwjgl3's release. Before replying to this issue however, I figured it might be a good idea to retest with latest JVMs and GC algorithms. Turns out, it is indeed possible to reproduce, even on JDK 8. My mistake in previous attempts was using debug mode to identify buffer cleanup (with breakpoints inside JDK) but debug mode effectively disables GC of unreachable local variables (makes sense in hindsight). This time I used normal execution and So, yeah, bad news so far. The good news is that it's extremely unlikely to happen in actual programs, for the following reasons:
We also cannot use Note that in lwjgl4 this is automatically handled for |
Question
I have not observed the crash in a real time scenario, but that might happen if a cleaner of a native resource deallocates it at the right moment.
The text was updated successfully, but these errors were encountered: