简体   繁体   中英

JNI DETECTED ERROR IN APPLICATION: use of deleted local reference 0x1

I've got Java function, which declaration looks like this:

public static void mathSendResults(final int kidId, final int points, final int correct, final int error, final float time,
                                       final String date, final long timestamp, final String description,
                                       final String settings, final int classNumber, final int level, final float percentage)

Now, I want to call this function through JNI:

void NativeHelper::mathSendResults(int kidId, int points, int correct, int error, float time,
                            std::string date, long timestamp, std::string description,
                            std::string settings, int classNumber, int level, float percentage) {

    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "mathSendResults",
                                                "(IIIIFLjava/lang/String;JLjava/lang/String;Ljava/lang/String;IIF)V")){

        jstring jdate = t.env->NewStringUTF(date.c_str());
        jstring jdescription = t.env->NewStringUTF(description.c_str());
        jstring jsettings = t.env->NewStringUTF(settings.c_str());

        t.env->CallStaticVoidMethod(t.classID, t.methodID, kidId, points, correct, error, time,
                                    jdate, timestamp, jdescription,
                                    jsettings, classNumber, level, percentage);

        t.env->DeleteLocalRef(t.classID);
        t.env->DeleteLocalRef(jdate);
        t.env->DeleteLocalRef(jdescription);
        t.env->DeleteLocalRef(jsettings);
    }
}

It should work, but app crashes with the following error:

JNI DETECTED ERROR IN APPLICATION: use of deleted local reference 0x1

Which looks very strange to me. I've tried removing DeleteLocalRef calls, but it still crashes. I've already have working other method, but with less parameters. I'm not sure if that's the reason. Anyway I've tried replacing string with ints (just for testing) so the number of parameters didn't change and it worked. So it's definitely an issue with string objects. I've also tried sending empty strings, but result is the same (so it's unrelated to string contents). I've also tried to reduce number of strings to just one, but it still crashes.

一个jmethodid不是一个jobject ,并不需要(也不能)被删除。

After a lot of trials&errors I've finally fixed it.

I don't know why, but when jstrings are first parameters it works. If I put something (like an int) before any string app will crash. Code below is the solution:

void NativeHelper::mathSendResults(int kidId, int points, int correct, int error, float time,
                            std::string date, long timestamp, std::string description,
                            std::string settings, int classNumber, int level, float percentage) {

//    final String date, final String description, final String settings,
//    final int kidId, final int points, final int correct, final int error, final int classNumber, final int level,
//    final float percentage, final float time,
//    final long timestamp

    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "mathSendResults",
                                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IIIIIIFFJ)V")){

        jstring jdate = t.env->NewStringUTF(date.c_str());
        jstring jdescription = t.env->NewStringUTF(description.c_str());
        jstring jsettings = t.env->NewStringUTF(settings.c_str());

        t.env->CallStaticVoidMethod(t.classID, t.methodID,
                                    jdate, jdescription, jsettings,
                                    kidId, points, correct, error, classNumber, level,
                                    percentage, time,
                                    timestamp);

        t.env->DeleteLocalRef(t.classID);
        t.env->DeleteLocalRef(jdate);
        t.env->DeleteLocalRef(jdescription);
        t.env->DeleteLocalRef(jsettings);
    }
}

Java:

public static void mathSendResults(final String date, final String description, final String settings,
                                   final int kidId, final int points, final int correct, final int error, final int classNumber, final int level,
                                   final float percentage, final float time,
                                   final long timestamp)

It's still a mystery to me why the order matters.

edit: Updated the code. Actually first version didn't work properly. Some int variables had incorrect (random) values in Java code. I had to sort parameters via type and then it finally worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM