Exception For No Apparent Reason

Originally posted at: Exception For No Apparent Reason · Issue #474 · jMonkeyEngine/jmonkeyengine · GitHub

I was recently testing my game and the following crash happened without apparent cause as a significant number of procedural geometries were attached:

java.lang.IllegalArgumentException: This NativeObject is not registered in this NativeObjectManager
    at com.jme3.util.NativeObjectManager.deleteNativeObject(NativeObjectManager.java:136)
    at com.jme3.util.NativeObjectManager.deleteUnused(NativeObjectManager.java:188)
    at com.jme3.renderer.opengl.GLRenderer.postFrame(GLRenderer.java:870)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:189)
    at com.jme3.system.lwjgl.LwjglCanvas.runLoop(LwjglCanvas.java:229)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:232)
    at java.lang.Thread.run(Thread.java:745)

In the past, when the exact same geometries (generated from the same seed) were attached, this issue didn’t occur. It also does not occur if I load those same geometries again.

Also see the SDK Issue for this: Assertion error when deleting native objects in the SDK · Issue #8 · jMonkeyEngine/sdk · GitHub
I don’t know if it’s related (it crashes 4 lines later), but if it turns out to be engine-relevant then it might also fix the SDK Issue.

Is it reproducable or does it only happen from time to time?

It only happened once and when I do the near-exact same thing it does not occur again. My game is multithreaded though so it is entirely possible that things are getting called in different orders. Also, it is, of course, impossible to be for example looking in exactly the same direction multiple times.

99 times out of 99 times, this is a threading issue. You have given something to the engine before you were done with it or freed it before it was done with it… or something of that sort.

I do a lot of crazy threading stuff and have never once seen this issue.

I just went through all of the scenegraph modifications in my entire game and all of them are either in an enqueue() or in an appstate’s methods (not stateAttached or stateDetached).

You may have missed something. This is a very tough kind of thing to track… even worse without being able to see the code.

I don’t really know how you are passing things off so I can’t really speculate. I don’t know if you are freeing things later, what your threads hold onto or don’t, etc…

The one thing we do know is that no one else has really reported this issue before… except as related to the SDK which does a lot of odd swing-related threading.

I mean, it “could” be a bug… but given that it’s pretty clearly induced by multithreading it would be impossible to track down outside of the app the exhibits the issue.

I had my IDE look for all instances of attachChild() and detachChild() – there are about 5 of each total and most of them weren’t even being used when the error occurred.

Nothing is passed to JME until my code is completely finished with it. It retains a reference to the Geometry to detach it later as needed, but, that is only accessed from the main thread. If you are concerned about something getting garbage collected, shouldn’t JME’s references to the object be enough even if my code lets all of it’s references go?

My game runs in a Swing window for various reasons. Perhaps it is a bug related to the swing canvas?