jni table overflow even after deleteLocalRef

前端 未结 2 1305
忘了有多久
忘了有多久 2021-01-12 19:10

When I run the code, I get an error \"failed adding to JNI local ref table has 512 entries\"

This is my code:

jstring pJNIData = pJNIEnv->NewStrin         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 19:29

    I thought I would chip in just in case anyone else runs into this issue. This is a weird case that kept me confused for hours!

    Ok so I have an NDK app and the Java code being called is inside an apk that is loaded at runtime. I have no idea if the runtime loading effects this in any way but I thought I should mention it.

    Now in a c++ method I use find class and getmethodid to get the constuctor to a HashMap and call it to get a new HashMap instance. I then populate the HashMap from the c++ side using jni calls. So far so good. I then pass the HashMap to java code and, again, all is working as expected. Once the java code has returned I call DeleteLocalRef on the HashMap. No errors are thrown but the reference is not deleted.

    This only came up when I finally ran over 512 local references (from multiple calls to this function) and the error dump showed that the last 10 items in the localref store were nearly all HashMaps. I would understand that the GC would not collect these references at the end of the method as I am make a multithreaded ndk app. However the DeleteLocalRef should have worked.

    The Fix: In the end I found that creating the HashMap from a jni call to a java method I wrote was fine, and the reference was then free'able. It seems crazy to have a java function that literally just returns a new HashMap but it worked so for now I am living with it :)

提交回复
热议问题