繁体   English   中英

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

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

我有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)

现在,我想通过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);
    }
}

它应该可以工作,但是应用程序崩溃并显示以下错误:

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

这对我来说很奇怪。 我尝试删除DeleteLocalRef调用,但仍然崩溃。 我已经在使用其他方法,但是参数较少。 我不确定这是否是原因。 无论如何,我都尝试用int替换字符串(仅用于测试),因此参数的数量没有变化并且有效。 因此,这绝对是字符串对象的问题。 我也尝试发送空字符串,但结果是相同的(因此与字符串内容无关)。 我还尝试将字符串数减少为一个,但是仍然崩溃。

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

经过反复试验和错误,我终于将其修复。

我不知道为什么,但是当jstrings是第一个参数时,它可以工作。 如果我在任何字符串应用程序崩溃前放了一些东西(如int)。 下面的代码是解决方案:

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)

对于订单为何重要,这仍然是我的一个谜。

编辑:更新了代码。 实际上,第一个版本无法正常运行。 一些int变量在Java代码中具有不正确的(随机)值。 我必须通过类型对参数进行排序,然后终于可以了。

暂无
暂无

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

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