Reputation: 1521
I have some JNI code in a thread that calls from Java to C. The code works but if I put too much pressure on it (execute too many instructions), it will crash:
W/dalvikvm( 1502): JNI local reference table summary (512 entries):
W/dalvikvm( 1502): 512 of Ljava/lang/Class; 164B (1 unique)
W/dalvikvm( 1502): Memory held directly by tracked refs is 164 bytes
E/dalvikvm( 1502): Failed adding to JNI local ref table (has 512 entries)
I/dalvikvm( 1502): "Thread-17" prio=5 tid=10 RUNNABLE
I/dalvikvm( 1502): | group="main" sCount=0 dsCount=0 s=N obj=0x40114910 self=0xd3d40
I/dalvikvm( 1502): | sysTid=1542 nice=0 sched=0/0 cgrp=default handle=821104
I/dalvikvm( 1502): | schedstat=( 359863282 630188006 2660 )
I/dalvikvm( 1502): at dalvik.system.NativeStart.run(Native Method)
I/dalvikvm( 1502):
E/dalvikvm( 1502): VM aborting
I/DEBUG ( 1011): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG ( 1011): Build fingerprint: 'verizon/voles/sholes/sholes:2.2.2/FRG83G/91102:user/release-keys'
I/DEBUG ( 1011): pid: 1502, tid: 1542 >>> app_process <<<
I/DEBUG ( 1011): signal 11 (SIGSEGV), fault addr deadd00d
I/DEBUG ( 1011): r0 00000026 r1 afd14629 r2 afd14629 r3 00000000
I/DEBUG ( 1011): r4 805a23f4 r5 805a23f4 r6 000d3d40 r7 000d3d90
I/DEBUG ( 1011): r8 00100000 r9 80601f45 10 449f1000 fp 4453ed90
I/DEBUG ( 1011): ip deadd00d sp 44af0dc0 lr afd15673 pc 805420b8 cpsr 20000030
In my call from Java to C, I keep a GlobalReference to the class I want to call and then create a new thread:
JNIEXPORT void JNICALL Java_com_device_client_HostConnection_initialize
(JNIEnv * env, jobject obj, jobject inject)
{
JavaVM *tmpVM;
jint result = env->GetJavaVM(&tmpVM);
if (result < 0) {
LOGE("Error using GetJavaVM\n");
exit(-1);
}
jobject tmpHostObject = env->NewGlobalRef(inject);
if (tmpHostObject == NULL) {
LOGE("ERROR: Run out of memory for weak global ref\n");
exit(-1);
}
vm = tmpVM;
hostObject = tmpHostObject;
pthread_t pth;
pthread_create(&pth, NULL, startServer, NULL);
pthread_join(pth, NULL);
}
Here vm and hostObject are global variables. In my new thread, I call a Java method from the inject object that I passed along and created a GobalRef to:
void *startServer(void* arg)
{
JNIEnv* env = NULL;
jmethodID mid;
jclass hostClass;
vm->AttachCurrentThread(&env, NULL);
while (run) {
/* some code */
/* Calls a new thread */
pthread_t pth;
pthread_create(&pth, NULL, receive, NULL);
/* more code */
}
vm->DetachCurrentThread();
}
void *receive()
{
jmethodID mid;
jclass hostClass;
JNIEnv* env = NULL;
vm->AttachCurrentThread(&env, NULL)
while (1) {
/* receive x, y and action */
hostClass = env->GetObjectClass(hostObject);
mid = env->GetMethodID(hostClass, "executeTouch", "(III)V");
if (mid == 0) {
LOGD("ERROR: GetMethodID\n");
exit(-1);
}
env->CallVoidMethod(hostObject, mid, x * 2, y * 2, action);
}
vm->DetachCurrentThread();
}
Does anyone please know what is going on? I am running out of memory please? Why would that be and how could I please avoid it?
EDIT:
I realized that I didn't need to get the MethodID all the time, I could just store it locally before the while (1). This fixed my problem! Sorry for the issue, thanks.
Thank you very much.
Upvotes: 2
Views: 2382
Reputation: 1521
I realized that I didn't need to get the MethodID all the time, I could just store it locally before the while (1). This fixed my problem! Sorry for the issue, thanks.
Upvotes: 1