我正在为Android制作游戏,其中我使用了一些JNI。过了一会儿我得到这个错误:
12-25 22:01:23.304:W / dalvikvm(13096):ReferenceTable溢出
(max = 1024)12-25 22:01:23.304:W / dalvikvm(13096):
中的最后10个条目JNI固定阵列参考表:12-25 22:01:23.304:
W / dalvikvm(13096):1014:0x4052ffd8 cls = [I(28 bytes)12-25
22:01:23.304:W / dalvikvm(13096):1015:0x4052fff8 cls = [I(28 bytes)
12-25 22:01:23.304:W / dalvikvm(13096):1016:0x40530018 cls = [I(28
bytes)12-25 22:01:23.304:W / dalvikvm(13096):1017:0x40530038 cls = [I
(28 bytes)12-25 22:01:23.304:W / dalvikvm(13096):1018:0x40530058 cls = [I(28 bytes)12-25 22:01:23.304:W / dalvikvm(13096):1019:
0x40530078 cls = [I(28 bytes)12-25 22:01:23.304:W / dalvikvm(13096):
1020:0x40530098 cls = [I(28 bytes)12-25 22:01:23.304:
W / dalvikvm(13096):1021:0x405300b8 cls = [I(28 bytes)12-25
22:01:23.304:W / dalvikvm(13096):1022:0x405300d8 cls = [I(28 bytes)
12-25 22:01:23.304:W / dalvikvm(13096):1023:0x405300f8 cls = [I(28
bytes)12-25 22:01:23.304:W / dalvikvm(13096):JNI固定阵列
参考表摘要(1024条目):12-25 22:01:23.314:
W / dalvikvm(13096):539 [I 28B(113独特)12-25 22:01:23.314:
W / dalvikvm(13096):140 [I 36B(28独特)12-25 22:01:23.314:
W / dalvikvm(13096):20 [I 44B(4独特)12-25 22:01:23.314:
W / dalvikvm(13096):325 [I 52B(65独特)12-25 22:01:23.314:
W / dalvikvm(13096):跟踪引用直接持有的内存为7728字节
我读过我必须释放内存(即ReleaseIntArrayElements),但在这种情况下我不知道如何做。我在java中有2d数组,我将它们传递给本机函数并本地转换为vector>。
jstring convert(JNIEnv *env, string ruch) {
jstring result;
char *writable = new char[ruch.size()+1];
copy(ruch.begin(), ruch.end(), writable);
writable[ruch.size()] = '\0';
result = env->NewStringUTF(writable);
delete[] writable;
return result;
}
JNIEXPORT jstring JNICALL Java_pl_game_yapapersoccer_Ndk_move
(JNIEnv *env, jclass obj, jint player, jint index, jint mode, jobjectArray matrix, jobjectArray matrixNeighbours, jobjectArray matrixWagi) {
int matrix_size = env->GetArrayLength(matrix);
vector<vector<int> > vMatrix(matrix_size);
for (int i=0;i<matrix_size;i++) {
vector<int> row(matrix_size);
vMatrix[i] = row;
jintArray oneDim= (jintArray)env->GetObjectArrayElement(matrix, i);
env->GetIntArrayRegion( oneDim, 0, matrix_size, &vMatrix[i][0] );
}
vector<vector<int> > vMatrixNeighbours(matrix_size);
for (int i=0;i<matrix_size;i++) {
jintArray oneDim= (jintArray)env->GetObjectArrayElement(matrixNeighbours, i);
jint *element=env->GetIntArrayElements(oneDim, 0);
int rowSize = env->GetArrayLength(oneDim);
vector<int> row(rowSize);
vMatrixNeighbours[i] = row;
env->GetIntArrayRegion( oneDim, 0, rowSize, &vMatrixNeighbours[i][0] );
}
vector<vector<int> > vMatrixWagi(matrix_size);
for (int i=0;i<matrix_size;i++) {
jintArray oneDim= (jintArray)env->GetObjectArrayElement(matrixWagi, i);
jint *element=env->GetIntArrayElements(oneDim, 0);
vector<int> row(2);
vMatrixWagi[i] = row;
env->GetIntArrayRegion( oneDim, 0, 2, &vMatrixWagi[i][0] );
}
// something something, ruch is c++ string, all legit
return convert(env,ruch);
}
这适用于前几个电话。可以理解释放对象的示例。除非完全必要,否则我想在java中保留2d数组,在c ++中保留向量向量。
答案 0 :(得分:0)
我知道如果你调用GetIntArrayElements
方法,你应该调用ReleaseIntArrayElements
来释放引用。您可以查看jni.h
文件以搜索名为Release **的方法。希望这可以帮助你。