java - 关闭/清除 Jni 连接

标签 java java-native-interface new-operator reset

有人知道是否有办法停止/重置 Java 应用程序与其 Jni 链接的 C++ 源之间的连接...?

我尝试通过调用对象的构造函数来重置该对象,但它不起作用......

MyJniJavaClass jni = new MyJniJavaClass();

---- MY ACTIONS ----

jni = new MyJniJavaClass();

---- OTHER ACTIONS ----

事实上,我的问题是我尝试访问我的 C++ DLL 两次。第一次还好,第二次就崩溃了…… 我尝试使用两个不同对象的实例访问 C++ DLL,但它仍然崩溃...

所以我认为问题是由于 C++ DLL 在我的应用程序内存中保持打开状态并且它不喜欢我访问它两次(“同时”)...

这是 C++ 代码:

////////////////// ADDED
#include "stdafx.h" 

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <XtremeAPI.h> 
////////////////// ADDED

#include "MainFrame_Jni_Functions.h"
#include <vector>
#include <string>
#include <iostream>

using namespace std;

// parameters table columns and row number
vector<char*>   paramName;
vector<char*>   paramType;
jdouble*        paramValue;
jdouble*        paramMin;
jdouble*        paramMax;
jdouble*        paramRef;
jint            paramNbRows;

// responses table columns and row number
vector<char*>   respName;
jint            respNbRows;

// objectives table columns and row number
vector<char*>   objName;
vector<char*>   objType;
jint            objNbRows;

// constraints table columns and row number
vector<char*>   conName;
vector<char*>   conType;
jdouble*        conLimit;
jdouble*        conExponent;
jdouble*        conRefVal;
jdouble*        conWeight;
jint            conNbRows;

// interval constraints table columns and row number
vector<char*>   intConName;
vector<char*>   intConType;
jdouble*        intConLowLimit;
jdouble*        intConUpLimit;
jdouble*        intConExponent;
jdouble*        intConWeight;
jint            intConNbRows;

// Xtreme data's
//static XtremeDataGA       xtremeDataGA;
/*XtremeDataFastGA  xtremeDataFastGA;
XtremeDataMGA       xtremeDataMGa;
XtremeDataFastGA    xtremeDataFastGA;
XtremeDataFastMGA   xtremeDataFastMGA;*/


JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_sendParamToNative
(   JNIEnv *env, jobject jobj, jobjectArray jparamName, jdoubleArray jparamValue, 
jdoubleArray jparamMin, jdoubleArray jparamMax, jdoubleArray jparamRef, jobjectArray jparamType){

    paramNbRows = env->GetArrayLength( jparamName );

    paramValue  = env->GetDoubleArrayElements(jparamValue, 0);
    paramMin    = env->GetDoubleArrayElements(jparamMin, 0);
    paramMax    = env->GetDoubleArrayElements(jparamMax, 0);
    paramRef    = env->GetDoubleArrayElements(jparamRef, 0);
    
    paramName   = vector<char*>(paramNbRows);
    for(int i=0; i<paramNbRows; i++){ 
        paramName.at(i) =  (char*)env->GetStringUTFChars(
            (jstring)env->GetObjectArrayElement( jparamName, i), 0 );
    }

    paramType   = vector<char*>(paramNbRows);
    for(int i=0; i<paramNbRows; i++){ 
        paramType.at(i) =  (char*)env->GetStringUTFChars(
            (jstring)env->GetObjectArrayElement( jparamType, i), 0 );
    }

    env->ReleaseDoubleArrayElements(jparamValue,paramValue, 0);
    env->ReleaseDoubleArrayElements(jparamMin,  paramMin,   0);
    env->ReleaseDoubleArrayElements(jparamMax,  paramMax,   0);
    env->ReleaseDoubleArrayElements(jparamRef,  paramRef,   0);
}

JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_sendRespToNative    (JNIEnv *env, jobject jobj, jobjectArray jRespName){        

respNbRows  = env->GetArrayLength( jRespName );

respName    = vector<char*>(respNbRows);
for(int i=0; i<respNbRows; i++){ 
    respName.at(i) =  (char*)env->GetStringUTFChars(
        (jstring)env->GetObjectArrayElement( jRespName, i), 0 );
}
}

JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_sendObjToNative  
(JNIEnv *env, jobject jobj, jobjectArray jObjName, jobjectArray jObjType){
            
objNbRows   = env->GetArrayLength( jObjName );

objName     = vector<char*>(objNbRows);
for(int i=0; i<objNbRows; i++){ 
    objName.at(i) =  (char*)env->GetStringUTFChars(
        (jstring)env->GetObjectArrayElement( jObjName, i), 0 );
}

objType     = vector<char*>(objNbRows);
for(int i=0; i<objNbRows; i++){ 
    objType.at(i) =  (char*)env->GetStringUTFChars(
        (jstring)env->GetObjectArrayElement( jObjType, i), 0 );
}
}

JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_sendConToNative
(   JNIEnv *env, jobject jobj, jobjectArray jConName, jobjectArray jConType, 
    jdoubleArray jConLimit, jdoubleArray jConExpo, jdoubleArray jConRefVal, jdoubleArray jConWeight){

        conNbRows       = env->GetArrayLength( jConName );

        conLimit        = env->GetDoubleArrayElements(jConLimit, 0);
        conExponent     = env->GetDoubleArrayElements(jConExpo, 0);
        conRefVal       = env->GetDoubleArrayElements(jConRefVal, 0);
        conWeight       = env->GetDoubleArrayElements(jConWeight, 0);

        conName         = vector<char*>(conNbRows);
        for(int i=0; i<conNbRows; i++){ 
            conName.at(i) =  (char*)env->GetStringUTFChars(
                (jstring)env->GetObjectArrayElement( jConName, i), 0 );
        }

        conType         = vector<char*>(conNbRows);
        for(int i=0; i<conNbRows; i++){ 
            conType.at(i) =  (char*)env->GetStringUTFChars(
                (jstring)env->GetObjectArrayElement( jConType, i), 0 );
        }

        env->ReleaseDoubleArrayElements(jConLimit,  conLimit,   0);
        env->ReleaseDoubleArrayElements(jConExpo,   conExponent,0);
        env->ReleaseDoubleArrayElements(jConRefVal, conRefVal,  0);
        env->ReleaseDoubleArrayElements(jConWeight, conWeight,  0);
}

JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_sendIntConToNative
(   JNIEnv *env, jobject jobj, jobjectArray jIntConName, jobjectArray jIntConType, 
    jdoubleArray jIntConLowLimit, jdoubleArray jIntConUpLimit, jdoubleArray jIntConExp, jdoubleArray jIntConWeight){

        intConNbRows = env->GetArrayLength( jIntConName );

        intConLowLimit  = env->GetDoubleArrayElements(jIntConLowLimit, 0);
        intConUpLimit   = env->GetDoubleArrayElements(jIntConUpLimit, 0);
        intConExponent  = env->GetDoubleArrayElements(jIntConExp, 0);
        intConWeight    = env->GetDoubleArrayElements(jIntConWeight, 0);

        intConName = vector<char*>(intConNbRows);
        for(int i=0; i<intConNbRows; i++){ 
            intConName.at(i) =  (char*)env->GetStringUTFChars(
                (jstring)env->GetObjectArrayElement( jIntConName, i), 0 );
        }

        intConType = vector<char*>(intConNbRows);
        for(int i=0; i<intConNbRows; i++){ 
            intConType.at(i) =  (char*)env->GetStringUTFChars(
                (jstring)env->GetObjectArrayElement( jIntConType, i), 0 );
        }

        env->ReleaseDoubleArrayElements(jIntConLowLimit,intConLowLimit, 0);
        env->ReleaseDoubleArrayElements(jIntConUpLimit, intConUpLimit,  0);
        env->ReleaseDoubleArrayElements(jIntConExp,     intConExponent, 0);
        env->ReleaseDoubleArrayElements(jIntConWeight,  intConWeight,   0);
}

JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_printTables  (JNIEnv *env, jobject jobj){

cout    << endl << "Printing from XtremeAPIProject" << endl;

cout    << endl << "paramTable" << endl;
for(int i=0; i<paramNbRows; i++)
    cout    << "line " << i << " : "    
                << " name=" << paramName.at(i)  
                << " val="  << paramValue[i]    
                << " min="  << paramMin[i] 
                << " max="  << paramMax[i]      
                << " ref="  << paramRef[i]
                << " type=" << paramType.at(i)          
            << endl;

cout    <<endl << "respTable" << endl;
for(int i=0; i<respNbRows; i++)
    cout    << "line " << i << " : "    
                << " name=" << respName.at(i)       
            << endl;

cout    <<endl << "objTable" << endl;
for(int i=0; i<objNbRows; i++)
    cout    << "line " << i << " : "    
                << " name=" << objName.at(i)        
                << " type=" << objType.at(i)    
            << endl;


cout    <<endl << "conTable" << endl;
for(int i=0; i<conNbRows; i++)
    cout    << "line " << i << " : "    
                << " name=" << conName.at(i)    
                << " type=" << conType.at(i)
                << " limit="<< conLimit[i] 
                << " exp="  << conExponent[i]       
                << " refV=" << conRefVal[i]
                << " wght=" << conWeight[i]         
            << endl;


cout    <<endl << "intConTable" << endl;
for(int i=0; i<intConNbRows; i++)
    cout    << "line " << i << " : "    
                << " name=" << intConName.at(i) 
                << " type=" << intConType.at(i)
                << " lowL=" << intConLowLimit[i] 
                << " uprL=" << intConUpLimit[i]     
                << " exp="  << intConExponent[i]
                << " wght=" << intConWeight[i]          
            << endl;
            
cout << endl << "-----------------------------------------------------------------------------" << endl;

return;
}

///////////////////////////////////////// test functions
// Test function
void rosenbrockFunction (std::vector<double>& parameterVector, std::vector<double>& responseVector)
{
double r = 0;

for (unsigned int i=0; i<parameterVector.size (); i++)
    if ( (i+1)%2 == 1 )
        r = r + pow(1.0E+00 - parameterVector.at (i), 2.0);
    else
        r = r + 100.0E+00 * 
        pow(parameterVector.at (i) - pow(parameterVector.at (i-1), 2.0), 2.0);

responseVector.at (0) = r;
}

// Test function
void zdt1Function (std::vector<double>& parameterVector, std::vector<double>& responseVector)
{
double nDouble = parameterVector.size ();

double f1 = parameterVector.at (0);
double g = 0.0;
for (unsigned int i=1; i<parameterVector.size (); i++)
    g += parameterVector.at (i);
g = 9.0 * g / (nDouble - 1.0);
g += 1.0;
double h = 1.0 - sqrt (f1 / g);

responseVector.at (0) = f1;
responseVector.at (0) = g * h;
}
////////////////////////////////////////////////////////


JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_runTestGA  (JNIEnv *env, jobject jobj){

//testGA();

//xtremeDataGA = XtremeDataGA();
XtremeDataGA xtremeData = XtremeDataGA();

//--------------- [ Parameters ] ----------------------------------------------------
xtremeData.parameters.resize (2); //paramNbRows

for(int i=0; i<2; i++){ //paramNbRows

    xtremeData.parameters.at(i).name        = paramName.at(i);
    xtremeData.parameters.at(i).valueMin    = paramMin[i];
    xtremeData.parameters.at(i).valueMax    = paramMax[i];
    xtremeData.parameters.at(i).valueRef    = paramRef[i];

    if      (!strcmp(paramType.at(i), "Double"))    
        xtremeData.parameters.at(i).valueType = 3;
    else if (!strcmp(paramType.at(i), "Integer"))
        xtremeData.parameters.at(i).valueType = 2;
    else if (!strcmp(paramType.at(i), "Boolean"))
        xtremeData.parameters.at(i).valueType = 1;
    else
        xtremeData.parameters.at(i).valueType = 0;
}

//--------------- [ Responses ] -----------------------------------------------------
xtremeData.responses.resize (1);

xtremeData.responses.at(0).name   = "F1";

//--------------- [ External simulation ] -------------------------------------------
xtremeData.externalSimulation.externalSimulationType    = 1;
xtremeData.externalSimulation.functionPointer           = &rosenbrockFunction;

//--------------- [ Objective function ] --------------------------------------------
xtremeData.penalties.resize (1);
xtremeData.penalties.at(0).quantityName = "F1";
xtremeData.penalties.at(0).penaltyType  = "MINIMIZE";
xtremeData.penalties.at(0).weight       = 1.0;

//--------------- [ Optimizer ] -----------------------------------------------------
xtremeData.optimizationData.initialSolutionStrategy = 0;
xtremeData.optimizationData.randomSeedStrategy      = 0;

xtremeData.optimizationGAData.reproductionCount = 50;
xtremeData.optimizationGAData.individualCount   = 50;

xtremeData.optimizationGAData.evaluationStrategy.first  = 1;
xtremeData.optimizationGAData.evaluationStrategy.second = 1;
xtremeData.optimizationGAData.GAType                    = 1; // our advance GA

xtremeData.optimizationData.solutionHistoryStrategy = 1;

bool success = XtremeAPIGA (xtremeData);

cout << endl << "success=" << success << endl;


cout << "nb1=" << xtremeData.resultsData.parameter.size() << endl;
cout << "nb2=" << xtremeData.resultsData.parameter.at(0).size() << endl;
cout << "nb3=" << xtremeData.resultsData.parameter.at(0).at(0).size() << endl;

/////////////////////////////////////////////////////////////////////////////////

cout << "begin!!!!!!!!!!!!!!" << endl;

cout << "before!!!!!!!!!!!!!!" << endl;
jclass jclazz = env->FindClass("MainFrame/Jni/TablesObjects");
cout << "after***************" << jclazz <<endl;


int iterationsSize = xtremeData.resultsData.parameter.size();
jdouble* jdbl = new jdouble(iterationsSize);
for(int i=0; i<iterationsSize; i++)
    jdbl[i] = xtremeData.resultsData.parameter.at(i).at(0).at(0);
jdoubleArray jdblArr = env->NewDoubleArray(iterationsSize);
env->SetDoubleArrayRegion(jdblArr, 0, iterationsSize, jdbl);


jclass jStringClass   = env->FindClass("java/lang/String"); 
jobjectArray jStringArray = env->NewObjectArray(6, jStringClass, 0);
for(int i=0; i<6; i++)
    env->SetObjectArrayElement(jStringArray, i, env->NewStringUTF("a") );


jmethodID mid = env->GetStaticMethodID(jclazz, "resultsParameterInsertCol", "([Ljava/lang/Object;[D)V");
env->CallStaticVoidMethod(jclazz, mid, jStringArray, jdblArr);

cout << "end!!!!!!!!!!!!!!" << endl;
}


JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_destroyAll  (JNIEnv *env, jobject jobj){

// parameters table columns and row number
paramName;
paramType;
paramValue = NULL;
paramMin = NULL;
paramMax = NULL;
paramRef = NULL;
paramNbRows;

// responses table columns and row number
respName;
respNbRows = NULL;

// objectives table columns and row number
objName;
objType;
objNbRows = NULL;

// constraints table columns and row number
conName;
conType;
conLimit = NULL;
conExponent = NULL;
conRefVal = NULL;
conWeight = NULL;
conNbRows = NULL;

// interval constraints table columns and row number
intConName;
intConType;
intConLowLimit = NULL;
intConUpLimit = NULL;
intConExponent = NULL;
intConWeight = NULL;
intConNbRows = NULL;
}

最佳答案

所有 native 库都会加载到 ClassLoader 的静态映射中,因此您不能简单地在一个 JVM 中重新加载 native 库。如何弄清楚如何使用这些东西,您可以查看 DLL 的接口(interface),它应该包含诸如 destroy、destroyN 方法之类的内容,以便库可以自行完成其资源。

关于java - 关闭/清除 Jni 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8293198/

相关文章:

java - BufferedReader.ready() 行为的差异

java - 将Android Facebook请求变成AsyncTask

java - JavaFx 中的 UI 没有响应

c++ - 为什么我们不能释放由 new 分配的内存?

java - 给定的 html 文件只提取有意义的文本

java - 在 java 中用 System.loadLibrary() 加载的 .dll 的搜索路径是什么?

java - 在 Java JNI 编程中使用 std::string 时的性能和内存使用情况

Java JNI 应用程序因堆栈溢出而崩溃

c# - 如何覆盖而不是隐藏 C# 子类中的成员变量(字段)?

javascript - 使用 new 运算符返回 javascript 函数会返回奇数范围