简体   繁体   English

JNI在应用程序中检测到错误:使用已删除的本地引用0x1

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

I've got Java function, which declaration looks like this: 我有Java函数,该声明如下所示:

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: 现在,我想通过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. 我尝试删除DeleteLocalRef调用,但仍然崩溃。 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. 无论如何,我都尝试用int替换字符串(仅用于测试),因此参数的数量没有变化并且有效。 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. 我不知道为什么,但是当jstrings是第一个参数时,它可以工作。 If I put something (like an int) before any string app will crash. 如果我在任何字符串应用程序崩溃前放了一些东西(如int)。 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: 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. 一些int变量在Java代码中具有不正确的(随机)值。 I had to sort parameters via type and then it finally worked. 我必须通过类型对参数进行排序,然后终于可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JNI检测到应用程序错误:使用已删除的弱全局引用 - JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference 是什么导致 JNI 错误“使用已删除的本地引用”? - What causes the JNI error "use of deleted local reference"? Android(ART)崩溃,错误JNI DETECTED ERROR IN APPLICATION:jstring是无效的本地引用 - Android (ART) crash with error JNI DETECTED ERROR IN APPLICATION: jstring is an invalid local reference 如何调试:应用程序中的JNI检测到错误:使用无效的jobject - How to debug: JNI DETECTED ERROR IN APPLICATION: use of invalid jobject Android(ART)崩溃,错误JNI DETECTED ERROR IN APPLICATION:jarray是无效的堆栈间接引用表或无效的引用 - Android (ART) crash with error JNI DETECTED ERROR IN APPLICATION: jarray is an invalid stack indirect reference table or invalid reference JNI在Brother SDK上的应用程序中检测到错误 - JNI DETECTED ERROR IN APPLICATION on Brother SDK Android JNI 在应用程序中检测到错误:调用 JNI GetMethodID 时出现未决异常 - Android JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception JNI 在应用程序中检测到错误:为内部类调用 NewObject 时使用无效的作业 - JNI DETECTED ERROR IN APPLICATION: use of invalid jobject when calling NewObject for innerclass SoundPoolThread 通过 JNI 错误导致 SIGSEGV“访问已删除的全局引用” - SoundPoolThread causing SIGSEGV via JNI ERROR "accessed deleted global reference" Tango应用程序突然断现JNI DETECTED ERROR IN APPLICATION - Tango app suddenly breaks with JNI DETECTED ERROR IN APPLICATION
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM