1

I'm implementing a callback function in C++, but I'm facing some leak problems.

The implementation of array creation:

jshortArray convertShortArrayToJNI(JNIEnv* env, short* arr, size_t count) {
  jshortArray retval = env->NewShortArray(count);
  env->SetShortArrayRegion(retval,0,count,arr);
  return retval;
}

Here the callback call:

short* audio_frame;
int size;
jobject* callbackObject;
jshortArray array = convertShortArrayToJNI(jenv, audio_frame, size);
LOGI("array %d", array);
jenv->CallVoidMethod(*callbackObject, callbackMI, array);
LOGI("callback called!");
jenv->ReleaseShortArrayElements(array, audio_frame, 0);

After some calls it's showed the error below:

12-03 11:12:38.425: W/dalvikvm(894): ReferenceTable overflow (max=512)
12-03 11:12:38.425: W/dalvikvm(894): Last 10 entries in JNI local reference table:
12-03 11:12:38.425: W/dalvikvm(894):   502: 0x44f879b0 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   503: 0x44f87a98 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   504: 0x44f87b80 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   505: 0x44f87c68 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   506: 0x44f87d50 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   507: 0x44f87e38 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   508: 0x44f87f20 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   509: 0x44f88008 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   510: 0x44f880f0 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894):   511: 0x44f881d8 cls=[S (100 bytes)
12-03 11:12:38.435: W/dalvikvm(894): JNI local reference table summary (512 entries):
12-03 11:12:38.435: W/dalvikvm(894):     1 of Ljava/lang/Class; 164B
12-03 11:12:38.435: W/dalvikvm(894):   511 of [S 100B (511 unique)
12-03 11:12:38.435: W/dalvikvm(894): Memory held directly by tracked refs is 51264 bytes

What is wrong?

Victor
  • 8,309
  • 14
  • 80
  • 129
  • Not entirely sure about this, but try removing the call to the ReleaseShortArrayElements. When you call NewShortArray, the array is now placed in Garbage Collected memory. SetShortArrayRegion simply copies memory from the buffer into the GC heap: freeing the memory you (not JNI) allocated is bound to cause problems. Use ReleaseShortArrayElements for GetShortArrayElements. – AStupidNoob Dec 03 '12 at 14:03

1 Answers1

7

This was the answer: ReferenceTable overflow (max=512) JNI

Calling jenv->DeleteLocalRef(array); solve my problem!

Community
  • 1
  • 1
Victor
  • 8,309
  • 14
  • 80
  • 129