2017-07-27 55 views
0

不知道dataArray~JniFloatArray是否被調用,當com/emcjpn/sleep/SleepAlgorithmBreakException被拋出?從JNI函數拋出java異常之後調用C++析構函數嗎?

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*/ 
    } 
+0

您是否嘗試在析構函數中放置斷點? – efekctive

+0

好主意。我會嘗試 –

回答

2

是的。在JNI文檔中很難找到,但env->ThrowNew實際上不會立即引發異常。相反,它設置了一些東西,以便在返回到Java-land後引發異常。

這意味着你需要跟隨ThrowNew返回某種類型(返回到Java-land),並且該返回將導致所有析構函數運行。

+1

在發佈的代碼中,在嘗試拋出NULL異常對象之前檢查對「FindClass()」的調用是否成功也可能是一個好主意。 –

相關問題