LWJGLX / debug

Debugging LWJGL3 OpenGL applications
MIT License
37 stars 7 forks source link

Exceptions when running the debug agent. #22

Closed Ramh5 closed 5 years ago

Ramh5 commented 5 years ago

My project works but when I add the agent to the vm arguments in eclipse, it seems to change the main thread and I get the following stack trace. Is it possible that using this debug tool changes my main thread so that my glfwInit is now called in another thread? Is there a way to fix that in my code?

[LWJGL] Version: 3.2.2 build 10
[LWJGL]      OS: Windows 10 v10.0
[LWJGL]     JRE: 11.0.2 amd64
[LWJGL]     JVM: OpenJDK 64-Bit Server VM v11.0.2+9 by Oracle Corporation
[LWJGL] Loading library (system): lwjgl
[LWJGL]     Using SharedLibraryLoader...
[LWJGL]     Found at: C:\Users\ramho\AppData\Local\Temp\lwjglramho\3.2.2-build-10\lwjgl.dll
[LWJGL]     Found at: C:\Users\ramho\AppData\Local\Temp\lwjglramho\3.2.2-build-10\lwjgl.dll
[LWJGL]     Loaded from org.lwjgl.librarypath: C:\Users\ramho\AppData\Local\Temp\lwjglramho\3.2.2-build-10\lwjgl.dll
[LWJGL] Java 9 stack walker enabled
[LWJGL] Warning: Failed to instantiate memory allocator: org.lwjgl.system.jemalloc.JEmallocAllocator. Using the system default.
[LWJGL] MemoryUtil allocator: DebugAllocator
[LWJGL] Loading library: glfw
[LWJGL]     Using SharedLibraryLoader...
[LWJGL]     Found at: C:\Users\ramho\AppData\Local\Temp\lwjglramho\3.2.2-build-10\glfw.dll
[LWJGL]     Loaded from org.lwjgl.librarypath: C:\Users\ramho\AppData\Local\Temp\lwjglramho\3.2.2-build-10\glfw.dll
[trace] (Window.java:57)                createPrint(java.io.PrintStream@d7fbb24) =  pointer [0x20E3F310000]
java.lang.IllegalStateException: Method glfwInit was called in thread [Thread[GAME_LOOP_THREAD,5,main]] which is not the main thread.
    at org.lwjglb.engine.Window.init(Window.java:60)
    at org.lwjglb.engine.GameEngine.init(GameEngine.java:62)
    at org.lwjglb.engine.GameEngine.run(GameEngine.java:52)
    at java.base/java.lang.Thread.run(Thread.java:834)
Exception in thread "GAME_LOOP_THREAD" java.lang.NullPointerException
    at org.lwjglb.game.DummyGame.cleanup(DummyGame.java:192)
    at org.lwjglb.engine.GameEngine.cleanup(GameEngine.java:96)
    at org.lwjglb.engine.GameEngine.run(GameEngine.java:57)
    at java.base/java.lang.Thread.run(Thread.java:834)
[LWJGL] 16 bytes leaked, thread 12 (GAME_LOOP_THREAD), address: 0x20E3F310000
    at org.lwjgl.system.Callback.create(Callback.java:133)
    at org.lwjgl.system.Callback.<init>(Callback.java:83)
    at org.lwjgl.glfw.GLFWErrorCallback.<init>(GLFWErrorCallback.java:60)
    at org.lwjgl.glfw.GLFWErrorCallback$1.<init>(GLFWErrorCallback.java:97)
    at org.lwjgl.glfw.GLFWErrorCallback.createPrint(GLFWErrorCallback.java:97)
    at org.lwjglx.debug.$Proxy$14.createPrint18(Unknown Source)
    at org.lwjglb.engine.Window.init(Window.java:57)
    at org.lwjglb.engine.GameEngine.init(GameEngine.java:62)
    at org.lwjglb.engine.GameEngine.run(GameEngine.java:52)
    at java.base/java.lang.Thread.run(Thread.java:834)
httpdigest commented 5 years ago

The exception means what it is saying: You are calling glfwInit in a thread other than the main thread. By definition, the main thread is the thread which initially calls your public static void main(String[] args) method. Somewhere in your code you are instantiating a new Thread and give it the name GAME_LOOP_THREAD. While calling GLFW methods (such as glfwInit and glfwCreateWindow) in threads other than the main thread may work on Windows, it will definitely fail on Mac OS. The debug agent makes sure a program will work on all OS'es. And calling glfwInit will not work on Mac OS. See the documentation of the GLFW functions, such as glfwInit (https://www.glfw.org/docs/latest/group__init.html#ga317aac130a235ab08c6db0834907d85e ) and look under the section "Thread safety".

Ramh5 commented 5 years ago

Thanks for the quick reply, we can close this question since I understand it is more a feature than a bug. As I knew what the GLFWInit documentation required it to run on the main thread, the code I got from a tutorial book is written like and is working on windows so I thought it was fine. I was more intrigued by the fact that it didn't work with the debug too activated. Now would it be desirable or even possible to have this debug tool only give you a warning that the code might not work on MacOS instead of crashing it?

Cheers.

httpdigest commented 5 years ago

Now would it be desirable or even possible to have this debug tool only give you a warning that the code might not work on MacOS instead of crashing it?

Yes, there is the nothrow Option. Please see the README.md of this project.

Ramh5 commented 5 years ago

Great many thanks for this tool.