GLFW crash on Macs

Getting the following error when running Spermination on a Mac. Using LWJGL 3, with the 64 “release” build. Trying to run with -XstartOnFirstThread seems to just hang that application. Error happens when clicking the “continue” button on the graphics configuration screen:

INFO: Running on jMonkeyEngine 3.1-5601

  • Branch: master
  • Git Hash: 2103b13
  • Build Date: 2015-12-22
    Dec 28, 2015 10:50:31 AM com.jme3.app.Application handleError
    SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
    java.lang.ExceptionInInitializerError
    at org.lwjgl.glfw.GLFW.glfwShowWindow(GLFW.java:1689)
    at com.jme3.system.lwjgl.LwjglWindow.createContext(LwjglWindow.java:211)
    at com.jme3.system.lwjgl.LwjglWindow.initInThread(LwjglWindow.java:287)
    at com.jme3.system.lwjgl.LwjglWindow.run(LwjglWindow.java:403)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.IllegalStateException: Please run the JVM with -XstartOnFirstThread and make sure a window toolkit other than GLFW (e.g. AWT or JavaFX) is not initialized.
    at org.lwjgl.glfw.EventLoop$OnScreen.(EventLoop.java:64)
    … 5 more

Dec 28, 2015 10:50:31 AM com.jme3.app.Application handleError
SEVERE: Failed to destroy context
java.lang.NullPointerException
at com.jme3.system.lwjgl.LwjglWindow.destroyContext(LwjglWindow.java:238)
at com.jme3.system.lwjgl.LwjglWindow.deinitInThread(LwjglWindow.java:387)
at com.jme3.system.lwjgl.LwjglWindow$3.uncaughtException(LwjglWindow.java:275)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1952)

KQ: Error reading KQ:Interrupted system call

Related LWJGL issue report:

-XstartOnFirstThread sounds required, but then why doesn’t anything appear?

EDIT: Might be the configuration screen not displaying, where the application is seeming to hang… for a temporary workaround, I’m going to try skipping the configuration screen to just play the game fullscreen.

Skipping the configuration screen doesn’t seem to be working. I still get the “use -XstartOnFirstThread & make sure AWT isn’t initialized” error. Is AWT getting initialized somewhere else…?

Maybe we need to apply this workaround from LibGDX:
https://github.com/libgdx/libgdx/blob/master/backends/gdx-backend-jglfw/src/com/badlogic/gdx/backends/jglfw/JglfwApplication.java#L119

Good find. I presume that should go right before context creation somewhere?

Before glfwInit() should be fine.

    if( Platform.get() == Platform.MACOSX ) java.awt.Toolkit.getDefaultToolkit();

… added before glfwInit doesn’t seem to be doing the trick. From [RFE] LWJGL 3.0

"The problem is, of course, the main thread situation in Mac OS X. GLFW, AWT, JavaFX, SWT, they’re all competing for that first thread to spawn the event loop on. Java has different behavior with different JVM versions ( has changed at least on every major release, including Java 8 ) and then there’s -XstartOnFirstThread. At least they have fixed AWT/JavaFX integration. Ideally, a modern Java app would never touch AWT (yes, including ImageIO), but currently LWJGL 3 assumes that you’d start with GLFW, without -XstartOnFirstThread, then go ahead and do something that initializes AWT (e.g. loading a texture image from disk). I had to implement two workarounds:

a) AWT is initialized automatically, before the native glfwInit is called. See org.lwjgl.system.macosx.EventLoop, can be overridden with -Dorg.lwjgl.macosx.nsloop.

b) Since the event loop is running on the AppKit thread and the LWJGL app is running on the main thread, we need to somehow transfer events between the two. This absolutely has to happen asynchronously (e.g. see glfwWaitEvents). The Disruptor is simply the highest performance solution to this problem. See org.lwjgl.system.glfw.WindowCallbackMacOSX for details. It’s also very useful for game programming in general. It’s designed for zero-allocation and is perfect for inter-process and inter-thread messaging, so I thought it’d be nice to bundle it with LWJGL.

All of the above is subject to change. This is the only difficult technical issue in LWJGL 3 and I currently don’t have enough Mac OS experience to figure out a satisfactory solution. Best I can do is provide different solutions for different use cases (and JVMs!)."

https://raw.githubusercontent.com/LWJGL/lwjgl3/74f5168d3bcc12251b34e51256b38b0d614df613/modules/core/src/main/java/org/lwjgl/glfw/EventLoop.java

… says it wants to be on the “main thread”. Doesn’t jME3 start a context on another thread, meaning this can’t be solved with a simple AWT call before glfwInit()? :confused:

I guess we can change this one bit for jME3 apps, e.g. to call application.run() instead of start() for example. Then the main thread can also run the game loop.

Is there any reason to start another thread? Should it be run() just when Macs are detected, or run() always?

Probably you can always start from the main thread. Creating a new thread was a thing from a long time ago (jME 0.x alpha from 2003) that sort of stayed.
There’s no reason to keep it if it breaks Macs though.

hrm… so where does the separate thread actually get created? Here in Application.start():

… it says it will execute it in a separate thread… but where does it get created? The new context call seems to go right into the LwjglWindow createContext, which includes the glfwInit()… :confused:

If I use -XstartOnMainThread, and make sure AWT isn’t started before glfwInit… the exception error seems to change to just “GLFW windows can only be started on the main thread”. I don’t have a Mac, so testing this is a slow process of finding a willing participant… Mac users could really help here :smile:

This commit on my branch will start a LWJGL 3 context on the same thread:

It needs to be used in conjunction with -XstartOnFirstThread argument on a Mac. This allows GLFW to run. Unfortunately, all I saw was a black screen… not sure what that was all about.

Like libGDX, you do need to initialize AWT before glfwInit like so:

This fixes the black screen. Unfortunately, the application texture size doesn’t seem to match the window’s size. For example, I see Spermination running in the lower-left corner of the window, about half the size of the window. The rest of the window blinks terribly. Might be an issue with the resize window callback?

Glad you find a solution on the thread issue.
About The size issue, I guess it’s due to HDPI on mac (retina). I had the same issue with jogl. The monitor should give you the scale to apply. In JOGL, there is a distinction between “window unit” and pixel (the surface unit), so every coordinate should take care of this (texture, mouse event,…)

I tried to apply your workaround and to run TestJaime (without settingsdialog) without success.

with fullscreen == false : nothing is displayed
with fullscreen == true : the resolution of the screen change (to match settings), and a flickering image is displayed, the mouse is working (size of the resolution, it is moving). I had to switch desktop and open a terminal to kill the process because I can’t switch app or “ESC”.

Sorry, I don’t know enough glfw/lwjgl3 to be usefull.

I fetched the latest change from on jmonkeyengine before, and used lwjgl-b35 like in jmonkeyengine.

The output of the run in the gits’comment : build.log · GitHub
(including the warning about deprecated OpenAL “This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API’s in AudioComponent.h.”)

OK, I’ve got a workaround working. It isn’t pretty, but it will allow jMonkeyEngine applications to run.

  1. Fullscreen doesn’t work… I have to bypass the startup configuration screen & just set a window, currently fixed @ 1280x720.

  2. You have to create a Mac application bundle, so certain options can be passed via a plist file. startOnFirstThread is the first one, and not including the high resolution plist option (which is how things are by default) will make the Mac upscale 1280x720 to fill the whole window without needing to increase the OpenGL texture resolution.

Since I use Java 8, you need to use certain files to get the Mac bundle application to find Java 8 (which will likely be Oracle’s “Internet Plugin”).

This is only a workaround, and not a solution. Users should be able to select a resolution & full screen modes, at least.

EDIT: I actually had to modify the universalJavaApplicationStub to not look at /usr/libexec/ for java, since it wouldn’t be the newest at that location.

1 Like

As a relatively new programming student I am getting this error as well, when I try to use LWJGL in Eclipse on a Macbook Pro. As I said, I am pretty new to programming, and all but the normal syntax is still alien to me. I wanted to try and make a game though as I learn. But I haven’t got a clue as to where I should even start to get this working.

phr00t, you mentioned to needed mac people to test with? if you still do I would be glad to help, and maybe then I can continue…

Doh! Came back to jME after a while and using 3.1.0-stable this is the first thing that hits my empty SimpleApp :frowning: Has anyone heard if there is a fix in lwjgl for this? Does it help to switch the renderer to JOGL, if that is still possible in 3.1.0?