title: JNI memory leak from byte array
body: <p>I have a java program that calls a native function many times over. My problem is that this function has a memory leak and everything that I do to get rid of it cause a memory dump. Any help would be greatly appreciated.</p>
  
  <p>This is my code </p>
  
  <pre><code>JNIEXPORT void JNICALL Java_class_method_getInput
  (JNIEnv *env, jobject obj)
  {
      if (inputIsAvailable)
      {
  
      int size = getBufferCurrentIndex();
      size -= getBufferReadIndex();
      size *= 2;
  
      char *finalSendArray = new char[size];
  
      getCommand(finalSendArray);
  
      jbyteArray byteArray = env-&gt;NewByteArray(size / 2);
      env-&gt;SetByteArrayRegion(byteArray, 0, size / 2, (jbyte*) finalSendArray);
  
      while(methodID == 0)
      {
          jclass cls = env-&gt;GetObjectClass(obj);
          methodID = env-&gt;GetMethodID(cls, "setCommand", "([B)V" );
      }
  
      env-&gt;CallVoidMethod(obj, methodID, byteArray);
  
      //env-&gt;ReleaseByteArrayElements(byteArray, (jbyte*) finalSendArray, JNI_ABORT);
  </code></pre>
  
  <p>My problem is that the above ^ code causes a memory dump if it is uncommented, if it is not uncommented my program runs out of memory in minutes</p>
  
  <pre><code>    env-&gt;DeleteLocalRef(byteArray);
      delete[] finalSendArray;
      }
  }
  </code></pre>
  
  <p>any help would be appreciated. Thanks!</p>