ReferenceTable溢出(最大= 1024)

时间:2013-05-03 13:56:16

标签: c++ opencv memory-management java-native-interface

我正在尝试在我的Android应用程序中使用JNI代码。我经常调用一个原生的mehod返回一个jfloatArray:

JNIEXPORT jfloatArray JNICALL Java_com_qualcomm_QCARSamples_ImageTargets_DetectionBasedTracker_getvaluenative
(JNIEnv *env, jclass, jlong thiz,jfloatArray feature)
{   //  these are my variables
   CvMat* cameraMat = cvCreateMat(3,3,CV_32F);
   CvMat* distMat =cvCreateMat(4,1,CV_32F);
   jfloat* flt1 = env->GetFloatArrayElements(feature,0);
   CvMat* imageMat = cvCreateMat(5,2,CV_32F);
   CvMat* objMat = cvCreateMat(5,3,CV_32F);
   CvMat* vec1 = cvCreateMat(1,3,CV_32F);
   CvMat* vec2 = cvCreateMat(1,3,CV_32F);
   Mat rotM ;
   Mat m(vec1);
   Mat t(vec2);
   Mat rotM1 = rotM.t();
   CvMat n = rotM1;
   CvMat translat= t;
   jfloatArray resultArray = env->NewFloatArray(12 );
   float matrice[12];
    env->SetFloatArrayRegion(resultArray, 0,12 ,matrice);

在操纵这些变量后,我将它们释放出来:

    cvReleaseMat(&vec1);
    cvReleaseMat(&vec2);
    cvReleaseMat(&distMat);
    cvReleaseMat(&cameraMat);
    cvReleaseMat(&imageMat);
    cvReleaseMat(&objMat);
    rotM.release();
    rotM1.release();
    m.release();
    t.release();
env->ReleaseFloatArrayElements(feature, flt1, 0);
    env->DeleteLocalRef(feature);
    return resultArray;
 }

我的应用程序崩溃,我在Logcat中有这个:

05-02 13:41:03.179: W/dalvikvm(11288): ReferenceTable overflow (max=1024)
05-02 13:41:03.179: W/dalvikvm(11288): JNI pinned array reference table (0x32e248) dump:
05-02 13:41:03.179: W/dalvikvm(11288):   Last 10 entries (of 1024):
05-02 13:41:03.179: W/dalvikvm(11288):      1023: 0x4105ad68 float[] (10 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1022: 0x4106d220 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1021: 0x41071db0 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1020: 0x4106d610 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1019: 0x4106d508 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1018: 0x4106efb0 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1017: 0x41072698 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1016: 0x410870c8 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1015: 0x4106cb88 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1014: 0x41087268 float[] (12 elements)
05-02 13:41:03.179: W/dalvikvm(11288):   Summary:
05-02 13:41:03.179: W/dalvikvm(11288):         1 of float[] (10 elements)
05-02 13:41:03.179: W/dalvikvm(11288):      1023 of float[] (12 elements) (1023 unique instances)
05-02 13:41:03.179: E/dalvikvm(11288): Failed adding to JNI pinned array ref table (1024 entries)

为什么发生溢出的任何想法?我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果要保留对返回的float数组的引用,则不会删除它们,并且您将达到JNI本地引用的限制。

来自评论中的其他方法:

param= (env->GetFloatArrayElements(imageArray,0)); 
then i have release it : env->DeleteLocalRef(imageArray);

您必须使用ReleaseFloatArrayElements而不是DeleteLocalRef来发布参数:

env->ReleaseFloatArrayElements(imageArray, param, 0);

此外,除非您也在使用Java进行一些处理,否则您应该只使用本机代码进行所有处理,而无需从Java到Native Code进行反复处理。在您的设计中,您似乎在本机代码中创建并填充数组,然后仅将其返回到Java以将其传递回另一个本机方法。除非有这样做的非常好的理由,否则您可以在本机代码中执行所有必需的处理,然后返回结果。