title: JNI NewByteArray memory leak
body: <p>I have a Java method that process a bitmap and returns a String.
  When I call this method from JNI (VS 2010) it works, but if I call this method many times,
  the memory of the process grown up until crash.
  The instruction that use a lot of memory is:</p>
  
  <pre><code>jbyteArray jBuff = _env-&gt;NewByteArray(b-&gt;Length);
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>static jobject staticArray=0;
  
  System::String^ MyClass::ExecuteJavaMethod(System::Drawing::Bitmap^ bmp)
  {
      JNIEnv *_env;
      System::String^ out;
      unsigned const char * buff;
  
      int res = jvm-&gt;AttachCurrentThread((void **)&amp;_env, NULL);
  
      if (jvm-&gt;GetEnv((void**) &amp;_env, JNI_VERSION_1_6) != JNI_OK)
      {
          return "GetEnv ERROR";
      }
  
      //save the bitmap in the stream
      MemoryStream^ ms = gcnew MemoryStream();
      bmp-&gt;Save(ms, ImageFormat::Bmp);
  
      //get the bitmap buffer
      array&lt;unsigned char&gt;^b = ms-&gt;GetBuffer() ;
  
      //unmanaged conversion
      buff = GetUnmanaged(b,b-&gt;Length);
  
  
      //fill the buffer
      jbyteArray jBuff = _env-&gt;NewByteArray(b-&gt;Length);       
      _env-&gt;SetByteArrayRegion(jBuff, 0, b-&gt;Length, (jbyte*) buff);
  
      //call the java method
      jstring str = (jstring) _env-&gt;CallStaticObjectMethod (  Main,
                                  javaMethod,
                                  jBuff);
  
  
  
      // _env-&gt;ReleaseByteArrayElements(jBuff,(jbyte*)buff), 0); //NOT WORKING
  
      //staticArray= _env-&gt;NewGlobalRef(jBuff);  NOT
      //_env-&gt;DeleteLocalRef(jBuff);             WORKING  
  
  
      //return the string result of the java method
      return gcnew String(env-&gt;GetStringUTFChars(str, 0));
  
  }
  </code></pre>