Unity3D for Android: “JNI ERROR (app bug): attempt to use stale global reference” with Incorrect Use of Prime31’s Social Networking Plugin

I was porting a Unity3D-built iOS game to Android the other day, while using the awesome Social Networking plugin from Prime31, and got this fun Java error:

E/dalvikvm( 5539): JNI ERROR (app bug): attempt to use stale global reference 0x32
E/dalvikvm( 5539): VM aborting
F/libc    ( 5539): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 5583 (UnityMain)

The above error crashed my app consistently.

I was lucky in this case because I was fairly sure where it’s coming from, suspecting an unnecessary call to FacebookAndroid.logout() inside a check for !FacebookAndroid.isSessionValid() check. Not my proudest coding moment, for sure.

Once I put a Debug.Log() right before the FacebookAndroid.logout() call and right after. The error changed to… or perhaps a new error started showing up around the same place in the debug log:

W/dalvikvm( 2910): threadid=9: thread exiting with uncaught exception (group=0x4001d5a0)
E/AndroidRuntime( 2910): FATAL EXCEPTION: GLThread 10
E/AndroidRuntime( 2910): java.lang.NullPointerException
E/AndroidRuntime( 2910): 	at com.prime31.FacebookPlugin.logout(FacebookPlugin.java:187)
E/AndroidRuntime( 2910): 	at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
E/AndroidRuntime( 2910): 	at com.unity3d.player.UnityPlayer.onDrawFrame(Unknown Source)
E/AndroidRuntime( 2910): 	at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
E/AndroidRuntime( 2910): 	at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
W/ActivityManager( 1323):   Force finishing activity com.companyName.MyBundleIDHere/com.unity3d.player.UnityPlayerNativeActivity

The app stopped crashing but the java.lang.NullPointerException didn’t go away either. Still looking into it.

Adding MySQL to $PATH Environment Variable on OS X

Update, 09.10.2012: This just worked for me for adding Android’s adb tool to my environment path on OS X 10.6.8. The first time I tried I got an error because I wrote “export $PATH=” instead of “export PATH=”.

Earlier this year I took some Java courses. I learned a ton from what was on the syllabus. It was exciting to see stuff like Generics in the Collections framework in JDK 1.5 and compare it to how Actionscript didn’t have that option until very recently, when the Vector class was added to AS3 for Flash Player 10.

One thing that caught me by surprise was that all software installation instructions for class materials were for Windows only (may be I was naive to expect otherwise). The professor was good but he wasn’t a Mac guy and he was super busy at his day job. A few of us OS X guys had to do extra work on our own on top of all the regular class work, just to get Eclipse, Tomcat & MySQL working on our systems. As painful as it was to spend extra days on things like “Why do I have to spend days on learning UNIX when my Java homework is due tomorrow?!!!” OR “Why do I keep getting a 404 Error for Servlets but not for JSP?! !@##$#^$%^$%^!!”… in the end, I think we ended up as slightly better developers, with a little bit more experience, because we all had to teach ourselves a little Terminal and OS X UNIX commands on the spot.

At the very least, I can always say, “Hey, at least I taught myself how to add “mysql” to the $PATH Environment Variable on OS X:

  1. launch Terminal (assuming it’s a bash shell)
  2. write ‘echo $PATH’ to see what your path is now
  3. if ‘mysql’ isn’t in there, write in the path to where you installed it via this command:
    ‘export PATH=/Users/your_user_name/your_path_to_mysql/mysql/bin:$PATH >> ~/.bash_profile’
  4. that should add it to your bash_profile
  5. write ‘echo $PATH’ again, you should see ‘mysql’ in there

Note: this is for installing MySQL as a stand alone app for use with J2EE/Java projects. If you’re just trying to do some PHP on a development machine, it’s easier to use a wysiwyg LAMP package like MAMP or XAMPP.

More on the $PATH environment variable here.

The same technique can be used to add the open source Flex SDK compiler, MXMLC, to your $PATH environment variable on OS X.