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 []
C []
C [] FT_Stream_Close+0x19
C [] FT_Stream_Free+0x25
C []
C [] FT_Done_Face+0x78
C []

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:

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

Thanks, guys.

No comments: