I’m new here so, hello everyone!
I’ve got an issue regarding program exiting.
I’m working in Intellij and to exit/stop the program, I have to press the stop button twice (first press sends SIGINT, and second press sends SIGKILL). I don’t like that because I want to gracefully exit the program and it started to get annoying to press the button twice.
Firstly, I added a custom SIGINT handler:
Signal.handle(new Signal("INT"), new SignalHandler() {
public void handle(Signal sig) {
System.out.println("SIGINT! Thread: "+Thread.currentThread().toString());
Engine.sigint();
System.exit(0);
}
});
Engine.sigint():
public static void sigint(){
if(!GLFW_initialized) return;
System.out.println("Engine | Processing SIGINT!");
glfwSetWindowShouldClose(glfwWindow, true);
System.out.println("Engine | SIGINT done!");
}
The rest of Engine (at least the GLFW and GL parts) are almost exactly the same as lwjgl.org/guide.
At the end of Engine.init() we have:
loop();
end();
Inside Engine.loop():
while(!glfwWindowShouldClose(glfwWindow)){
...
}
Engine.end():
private static void end(){
System.out.println("Stopped loop, destroying GLFW!");
glfwFreeCallbacks(glfwWindow);
glfwDestroyWindow(glfwWindow);
glfwTerminate();
glfwSetErrorCallback(null).free();
}
Now, after investigation:
- When pressing the “X” on the window, the program exits, the SIGINT code is never ran (what is ok), and the program exists with code 0.
Console:
Stopped loop, destroying GLFW!
Process finished with exit code 0
- When I stop the program by pressing the stop button in Intellij (sending SIGINT), the SIGINT code runs, but the glfwWindowShouldClose flag isn’t set despite calling
glfwSetWindowShouldClose(glfwWindow, true), thus continuing the loop and never exiting.
Console:
SIGINT! Thread: Thread[SIGINT handler,9,system]
Engine | Processing SIGINT!
Engine | SIGINT done!
Issue conclusion (might not be right, this is what I’m thinking) / TL;DR:
- glfwSetWindowShouldClose(window, true) doesn’t set the flag to true
I hope I didn’t misread the documentation or am missing something. The JavaDoc says exactly what I want to happen and that the method can be called from any thread.
Edit 1:
I’m working on Linux