Crash while returning from android ndk function on Samsung Galaxy S2.
In my java code I call an NDK function, and sometimes (this may be one of 20 random cases) the application hangs (ANR) immediately after returning from NDK. The log says
12-05 04:19:59.760: D/AndroidRuntime(5607): Shutting down VM 12-05 04:19:59.760: W/dalvikvm(5607): threadid=1: thread exiting with uncaught exception (group=0x40015578) 12-05 04:19:59.775: W/dalvikvm(5607): threadid=14: thread exiting with uncaught exception (group=0x40015578)
Sometimes a few lines of java code are executed before this happens. I’ve tried fully clearing the NDK function and removing all params from it to no avail. The function is called from a separate java thread.
On other phones (HTC Desire HD, HTC Legend and others) I have not seen this problem.
This is my java code, which calls ndk function:
private static native void ndkFunctionN(
byte[] filePath1N, byte[] filePath2N,
byte[] filePath3N, byte[] tmpDirN, JavaClass callerN,
byte[] param5N, byte[] param6N, byte[] param7N);
public static void javaFunction(
File file1, File file2, File file3, String tmpDir,
JavaClass caller, String param5, String param6, String param7)
{
try {
byte[] filePath1N = file1.getPath().getBytes("UTF-8");
byte[] filePath2N = file2.getPath().getBytes("UTF-8");
byte[] filePath3N = file3.getPath().getBytes("UTF-8");
byte[] tmpDirN = tmpDir.getBytes("UTF-8");
byte[] param5N = appClassName.getBytes("UTF-8");
byte[] param6N = param6.getBytes("UTF-8");
byte[] param7N = param7.getBytes("UTF-8");
mergeAndDumpN(
filePath1N, filePath2N, filePath3N, tmpDirN,
caller, param5N, param6N, param7N);
} catch (UnsupportedEncodingException e) {
MyLog.e(e);
}
}
This is the C++ code:
JNIEXPORT void JNICALL Java_com_package_name_ClassName_ndkFunctionN (
JNIEnv* env, jobject thiz,
jbyteArray filePath1N, jbyteArray filePath2N,
jbyteArray filePath3N, jbyteArray tmpDirN,
jobject callerN, jbyteArray param5N,
jbyteArray param6N, jbyteArray param7N)
{
}
Perhaps, in your separate thread you are still using the same JNIEnv value as you got in your first thread? You have to call
g_JavaVM->AttachCurrentThread(&env, NULL);or something similar to obtain a correct JNIEnv for your other thread. The same way you should then to refind all your classes/methods using that new JNIEnv value