从JNI函数抛出java异常后调用C ++析构函数?

时间:2017-07-27 15:18:43

标签: java c++ java-native-interface

我想知道在~JniFloatArray投掷时是否dataArraycom/emcjpn/sleep/SleepAlgorithmBreakException调用了吗?

JNIEXPORT jobject JNICALL    Java_com_emcjpn_sleep_SleepAlgorithm_nativePushNewDataAndCalculate(JNIEnv *env, jclass type,
                                                                       jlong ptr, jfloatArray data_) {
        JniFloatArray dataArray(data_, env);
        jfloat *data = dataArray.getData();

        SleepAlgorithm* algorithm = (SleepAlgorithm*)ptr;
        jsize length = dataArray.length();
        SleepAlgorithmResult result = algorithm->pushNewDataAndCalculate(data, data + length);
        if (result.shouldBreak) {
            jclass exception = env->FindClass("com/emcjpn/sleep/SleepAlgorithmBreakException");
            env->ThrowNew(exception, "sleep calculation failed, invalid ecg data");
            return NULL;
        }

        /*Some other code*/
    }

1 个答案:

答案 0 :(得分:2)

是。在JNI文档中很难找到,但env->ThrowNew实际上并没有立即抛出异常。相反,它会进行设置,以便在您返回Java-land时抛出异常。

这意味着你必须跟随ThrowNew返回某种类型(返回Java-land),并且该返回将导致所有析构函数运行。