I am experiencing a strange problem. I have a JNI code (very simplified for clarity) that goes as follows
void Foo::Bar(int count)
{
....
jclass javaClass1 = (jclass)env->FindClass("MyJavaClass1");
jmethodID constrObject1 = env->GetMethodID(javaClass1, "<init>", "()V");
...
jclass javaClassN = (jclass)env->FindClass("MyJavaClassN");
jmethodID constrObjectN = env->GetMethodID(javaClassN, "<init>", "()V");
for (int i = 0; i < count; i++)
{
env->PushLocalFrame(256 ) ;
jobject jniObject1 = env->NewObject(javaClass1, constrObject1);
...
jobject jniObjectN = env->NewObject(javaClassN, constrObjectN);
doSomething(<some objects created above>);
doSomethingElse(<some objects created above>);
doSomethingThird(<some objects created above>);
env->PopLocalFrame(0);
}
}
This code is working fine when compiled with CLang for Android API 27 but crashes when compiled with gcc for Android API 21 with the error
01-01 03:23:26.714 715 715 F DEBUG : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
01-01 03:23:26.736 715 715 F DEBUG : Abort message: 'art/runtime/indirect_reference_table.cc:115] JNI ERROR (app bug): local reference table overflow (max=512)'
I know what this error means and use PushLocalFrame/PopLocalFrame to prevent it. What I am asking is: why this code works with CLang but doesn't work with gcc? Am I missing something? Or, probably, the issue is API 21 vs API 27?
I have found this question and this question, they look related to my problem, but they are old and probably irrelevant. I would be grateful for any pointers.