Wednesday, December 31, 2008

JNI crashes in FontManager code

I hit a JVM crash happening in the Java 2D font manager code. Here's the stack trace at the crash point:

Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V [libjvm.so+0x298366]
C [libfontmanager.so+0x59e4]
C [libfreetype.so.6+0x73c9] FT_Stream_Close+0x19
C [libfreetype.so.6+0xa065] FT_Stream_Free+0x25
C [libfreetype.so.6+0xa6e2]
C [libfreetype.so.6+0xaf78] FT_Done_Face+0x78
C [libfontmanager.so+0x6964]
Java_sun_font_FreetypeFontScaler_disposeNativeScaler+0x34

This appeared to be due to two JNI pitfalls:
  • The JNIEnv is unique to the thread. It cannot be saved by one thread and reused by another. Use GetEnv instead.
  • The font2D jobject in freetypeScaler.c needs to be converted into a global reference because its lifetime exceeds the lifetime of a native method call.
BTW, here's a link to one of the JNI references:

http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html

Martin Buchholz and I suggested a patch. But here's a fix that was actually submitted by Igor Nekrestyanov:

http://hg.openjdk.java.net/jdk7/2d/jdk/rev/11d333de082f

Thanks, guys.

No comments: