title: Closing/Clearing Jni connections
body: <p>Anyone knows if there's a way to stop/reset the connection between a Java application and it's Jni linked C++ source... ?</p>
  <p>I tried resetting the object by calling its constructor, but it doesn't work ...</p>
  <blockquote>
  <p>MyJniJavaClass jni = new MyJniJavaClass();</p>
  <p>---- MY ACTIONS ----</p>
  <p>jni = new MyJniJavaClass();</p>
  <p>---- OTHER ACTIONS ----</p>
  </blockquote>
  <p>In fact, my problem is that I try to access my C++ DLL two times. It works well the first time and then crashes the second one...
  I tried accessing the C++ DLL using two different object's instances but it still crashes ...</p>
  <p>So I suppose that the problem is due to the fact that the C++ DLL stays open in my application memory and that it doesn't like me to access it twice (&quot;at the same time&quot;)...</p>
  <p>Here is the C++ code :</p>
  <pre><code>////////////////// ADDED
  #include &quot;stdafx.h&quot; 
  
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;math.h&gt;
  #include &lt;XtremeAPI.h&gt; 
  ////////////////// ADDED
  
  #include &quot;MainFrame_Jni_Functions.h&quot;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  // parameters table columns and row number
  vector&lt;char*&gt;   paramName;
  vector&lt;char*&gt;   paramType;
  jdouble*        paramValue;
  jdouble*        paramMin;
  jdouble*        paramMax;
  jdouble*        paramRef;
  jint            paramNbRows;
  
  // responses table columns and row number
  vector&lt;char*&gt;   respName;
  jint            respNbRows;
  
  // objectives table columns and row number
  vector&lt;char*&gt;   objName;
  vector&lt;char*&gt;   objType;
  jint            objNbRows;
  
  // constraints table columns and row number
  vector&lt;char*&gt;   conName;
  vector&lt;char*&gt;   conType;
  jdouble*        conLimit;
  jdouble*        conExponent;
  jdouble*        conRefVal;
  jdouble*        conWeight;
  jint            conNbRows;
  
  // interval constraints table columns and row number
  vector&lt;char*&gt;   intConName;
  vector&lt;char*&gt;   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-&gt;GetArrayLength( jparamName );
  
      paramValue  = env-&gt;GetDoubleArrayElements(jparamValue, 0);
      paramMin    = env-&gt;GetDoubleArrayElements(jparamMin, 0);
      paramMax    = env-&gt;GetDoubleArrayElements(jparamMax, 0);
      paramRef    = env-&gt;GetDoubleArrayElements(jparamRef, 0);
      
      paramName   = vector&lt;char*&gt;(paramNbRows);
      for(int i=0; i&lt;paramNbRows; i++){ 
          paramName.at(i) =  (char*)env-&gt;GetStringUTFChars(
              (jstring)env-&gt;GetObjectArrayElement( jparamName, i), 0 );
      }
  
      paramType   = vector&lt;char*&gt;(paramNbRows);
      for(int i=0; i&lt;paramNbRows; i++){ 
          paramType.at(i) =  (char*)env-&gt;GetStringUTFChars(
              (jstring)env-&gt;GetObjectArrayElement( jparamType, i), 0 );
      }
  
      env-&gt;ReleaseDoubleArrayElements(jparamValue,paramValue, 0);
      env-&gt;ReleaseDoubleArrayElements(jparamMin,  paramMin,   0);
      env-&gt;ReleaseDoubleArrayElements(jparamMax,  paramMax,   0);
      env-&gt;ReleaseDoubleArrayElements(jparamRef,  paramRef,   0);
  }
  
  JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_sendRespToNative    (JNIEnv *env, jobject jobj, jobjectArray jRespName){        
  
  respNbRows  = env-&gt;GetArrayLength( jRespName );
  
  respName    = vector&lt;char*&gt;(respNbRows);
  for(int i=0; i&lt;respNbRows; i++){ 
      respName.at(i) =  (char*)env-&gt;GetStringUTFChars(
          (jstring)env-&gt;GetObjectArrayElement( jRespName, i), 0 );
  }
  }
  
  JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_sendObjToNative  
  (JNIEnv *env, jobject jobj, jobjectArray jObjName, jobjectArray jObjType){
              
  objNbRows   = env-&gt;GetArrayLength( jObjName );
  
  objName     = vector&lt;char*&gt;(objNbRows);
  for(int i=0; i&lt;objNbRows; i++){ 
      objName.at(i) =  (char*)env-&gt;GetStringUTFChars(
          (jstring)env-&gt;GetObjectArrayElement( jObjName, i), 0 );
  }
  
  objType     = vector&lt;char*&gt;(objNbRows);
  for(int i=0; i&lt;objNbRows; i++){ 
      objType.at(i) =  (char*)env-&gt;GetStringUTFChars(
          (jstring)env-&gt;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-&gt;GetArrayLength( jConName );
  
          conLimit        = env-&gt;GetDoubleArrayElements(jConLimit, 0);
          conExponent     = env-&gt;GetDoubleArrayElements(jConExpo, 0);
          conRefVal       = env-&gt;GetDoubleArrayElements(jConRefVal, 0);
          conWeight       = env-&gt;GetDoubleArrayElements(jConWeight, 0);
  
          conName         = vector&lt;char*&gt;(conNbRows);
          for(int i=0; i&lt;conNbRows; i++){ 
              conName.at(i) =  (char*)env-&gt;GetStringUTFChars(
                  (jstring)env-&gt;GetObjectArrayElement( jConName, i), 0 );
          }
  
          conType         = vector&lt;char*&gt;(conNbRows);
          for(int i=0; i&lt;conNbRows; i++){ 
              conType.at(i) =  (char*)env-&gt;GetStringUTFChars(
                  (jstring)env-&gt;GetObjectArrayElement( jConType, i), 0 );
          }
  
          env-&gt;ReleaseDoubleArrayElements(jConLimit,  conLimit,   0);
          env-&gt;ReleaseDoubleArrayElements(jConExpo,   conExponent,0);
          env-&gt;ReleaseDoubleArrayElements(jConRefVal, conRefVal,  0);
          env-&gt;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-&gt;GetArrayLength( jIntConName );
  
          intConLowLimit  = env-&gt;GetDoubleArrayElements(jIntConLowLimit, 0);
          intConUpLimit   = env-&gt;GetDoubleArrayElements(jIntConUpLimit, 0);
          intConExponent  = env-&gt;GetDoubleArrayElements(jIntConExp, 0);
          intConWeight    = env-&gt;GetDoubleArrayElements(jIntConWeight, 0);
  
          intConName = vector&lt;char*&gt;(intConNbRows);
          for(int i=0; i&lt;intConNbRows; i++){ 
              intConName.at(i) =  (char*)env-&gt;GetStringUTFChars(
                  (jstring)env-&gt;GetObjectArrayElement( jIntConName, i), 0 );
          }
  
          intConType = vector&lt;char*&gt;(intConNbRows);
          for(int i=0; i&lt;intConNbRows; i++){ 
              intConType.at(i) =  (char*)env-&gt;GetStringUTFChars(
                  (jstring)env-&gt;GetObjectArrayElement( jIntConType, i), 0 );
          }
  
          env-&gt;ReleaseDoubleArrayElements(jIntConLowLimit,intConLowLimit, 0);
          env-&gt;ReleaseDoubleArrayElements(jIntConUpLimit, intConUpLimit,  0);
          env-&gt;ReleaseDoubleArrayElements(jIntConExp,     intConExponent, 0);
          env-&gt;ReleaseDoubleArrayElements(jIntConWeight,  intConWeight,   0);
  }
  
  JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_printTables  (JNIEnv *env, jobject jobj){
  
  cout    &lt;&lt; endl &lt;&lt; &quot;Printing from XtremeAPIProject&quot; &lt;&lt; endl;
  
  cout    &lt;&lt; endl &lt;&lt; &quot;paramTable&quot; &lt;&lt; endl;
  for(int i=0; i&lt;paramNbRows; i++)
      cout    &lt;&lt; &quot;line &quot; &lt;&lt; i &lt;&lt; &quot; : &quot;    
                  &lt;&lt; &quot; name=&quot; &lt;&lt; paramName.at(i)  
                  &lt;&lt; &quot; val=&quot;  &lt;&lt; paramValue[i]    
                  &lt;&lt; &quot; min=&quot;  &lt;&lt; paramMin[i] 
                  &lt;&lt; &quot; max=&quot;  &lt;&lt; paramMax[i]      
                  &lt;&lt; &quot; ref=&quot;  &lt;&lt; paramRef[i]
                  &lt;&lt; &quot; type=&quot; &lt;&lt; paramType.at(i)          
              &lt;&lt; endl;
  
  cout    &lt;&lt;endl &lt;&lt; &quot;respTable&quot; &lt;&lt; endl;
  for(int i=0; i&lt;respNbRows; i++)
      cout    &lt;&lt; &quot;line &quot; &lt;&lt; i &lt;&lt; &quot; : &quot;    
                  &lt;&lt; &quot; name=&quot; &lt;&lt; respName.at(i)       
              &lt;&lt; endl;
  
  cout    &lt;&lt;endl &lt;&lt; &quot;objTable&quot; &lt;&lt; endl;
  for(int i=0; i&lt;objNbRows; i++)
      cout    &lt;&lt; &quot;line &quot; &lt;&lt; i &lt;&lt; &quot; : &quot;    
                  &lt;&lt; &quot; name=&quot; &lt;&lt; objName.at(i)        
                  &lt;&lt; &quot; type=&quot; &lt;&lt; objType.at(i)    
              &lt;&lt; endl;
  
  
  cout    &lt;&lt;endl &lt;&lt; &quot;conTable&quot; &lt;&lt; endl;
  for(int i=0; i&lt;conNbRows; i++)
      cout    &lt;&lt; &quot;line &quot; &lt;&lt; i &lt;&lt; &quot; : &quot;    
                  &lt;&lt; &quot; name=&quot; &lt;&lt; conName.at(i)    
                  &lt;&lt; &quot; type=&quot; &lt;&lt; conType.at(i)
                  &lt;&lt; &quot; limit=&quot;&lt;&lt; conLimit[i] 
                  &lt;&lt; &quot; exp=&quot;  &lt;&lt; conExponent[i]       
                  &lt;&lt; &quot; refV=&quot; &lt;&lt; conRefVal[i]
                  &lt;&lt; &quot; wght=&quot; &lt;&lt; conWeight[i]         
              &lt;&lt; endl;
  
  
  cout    &lt;&lt;endl &lt;&lt; &quot;intConTable&quot; &lt;&lt; endl;
  for(int i=0; i&lt;intConNbRows; i++)
      cout    &lt;&lt; &quot;line &quot; &lt;&lt; i &lt;&lt; &quot; : &quot;    
                  &lt;&lt; &quot; name=&quot; &lt;&lt; intConName.at(i) 
                  &lt;&lt; &quot; type=&quot; &lt;&lt; intConType.at(i)
                  &lt;&lt; &quot; lowL=&quot; &lt;&lt; intConLowLimit[i] 
                  &lt;&lt; &quot; uprL=&quot; &lt;&lt; intConUpLimit[i]     
                  &lt;&lt; &quot; exp=&quot;  &lt;&lt; intConExponent[i]
                  &lt;&lt; &quot; wght=&quot; &lt;&lt; intConWeight[i]          
              &lt;&lt; endl;
              
  cout &lt;&lt; endl &lt;&lt; &quot;-----------------------------------------------------------------------------&quot; &lt;&lt; endl;
  
  return;
  }
  
  ///////////////////////////////////////// test functions
  // Test function
  void rosenbrockFunction (std::vector&lt;double&gt;&amp; parameterVector, std::vector&lt;double&gt;&amp; responseVector)
  {
  double r = 0;
  
  for (unsigned int i=0; i&lt;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&lt;double&gt;&amp; parameterVector, std::vector&lt;double&gt;&amp; responseVector)
  {
  double nDouble = parameterVector.size ();
  
  double f1 = parameterVector.at (0);
  double g = 0.0;
  for (unsigned int i=1; i&lt;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&lt;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), &quot;Double&quot;))    
          xtremeData.parameters.at(i).valueType = 3;
      else if (!strcmp(paramType.at(i), &quot;Integer&quot;))
          xtremeData.parameters.at(i).valueType = 2;
      else if (!strcmp(paramType.at(i), &quot;Boolean&quot;))
          xtremeData.parameters.at(i).valueType = 1;
      else
          xtremeData.parameters.at(i).valueType = 0;
  }
  
  //--------------- [ Responses ] -----------------------------------------------------
  xtremeData.responses.resize (1);
  
  xtremeData.responses.at(0).name   = &quot;F1&quot;;
  
  //--------------- [ External simulation ] -------------------------------------------
  xtremeData.externalSimulation.externalSimulationType    = 1;
  xtremeData.externalSimulation.functionPointer           = &amp;rosenbrockFunction;
  
  //--------------- [ Objective function ] --------------------------------------------
  xtremeData.penalties.resize (1);
  xtremeData.penalties.at(0).quantityName = &quot;F1&quot;;
  xtremeData.penalties.at(0).penaltyType  = &quot;MINIMIZE&quot;;
  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 &lt;&lt; endl &lt;&lt; &quot;success=&quot; &lt;&lt; success &lt;&lt; endl;
  
  
  cout &lt;&lt; &quot;nb1=&quot; &lt;&lt; xtremeData.resultsData.parameter.size() &lt;&lt; endl;
  cout &lt;&lt; &quot;nb2=&quot; &lt;&lt; xtremeData.resultsData.parameter.at(0).size() &lt;&lt; endl;
  cout &lt;&lt; &quot;nb3=&quot; &lt;&lt; xtremeData.resultsData.parameter.at(0).at(0).size() &lt;&lt; endl;
  
  /////////////////////////////////////////////////////////////////////////////////
  
  cout &lt;&lt; &quot;begin!!!!!!!!!!!!!!&quot; &lt;&lt; endl;
  
  cout &lt;&lt; &quot;before!!!!!!!!!!!!!!&quot; &lt;&lt; endl;
  jclass jclazz = env-&gt;FindClass(&quot;MainFrame/Jni/TablesObjects&quot;);
  cout &lt;&lt; &quot;after***************&quot; &lt;&lt; jclazz &lt;&lt;endl;
  
  
  int iterationsSize = xtremeData.resultsData.parameter.size();
  jdouble* jdbl = new jdouble(iterationsSize);
  for(int i=0; i&lt;iterationsSize; i++)
      jdbl[i] = xtremeData.resultsData.parameter.at(i).at(0).at(0);
  jdoubleArray jdblArr = env-&gt;NewDoubleArray(iterationsSize);
  env-&gt;SetDoubleArrayRegion(jdblArr, 0, iterationsSize, jdbl);
  
  
  jclass jStringClass   = env-&gt;FindClass(&quot;java/lang/String&quot;); 
  jobjectArray jStringArray = env-&gt;NewObjectArray(6, jStringClass, 0);
  for(int i=0; i&lt;6; i++)
      env-&gt;SetObjectArrayElement(jStringArray, i, env-&gt;NewStringUTF(&quot;a&quot;) );
  
  
  jmethodID mid = env-&gt;GetStaticMethodID(jclazz, &quot;resultsParameterInsertCol&quot;, &quot;([Ljava/lang/Object;[D)V&quot;);
  env-&gt;CallStaticVoidMethod(jclazz, mid, jStringArray, jdblArr);
  
  cout &lt;&lt; &quot;end!!!!!!!!!!!!!!&quot; &lt;&lt; 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;
  }
  </code></pre>