Referen to:http://blog.csdn.net/rainlight/article/details/818964
In Sun's official documentation, the use of this function is as follows
The array is returned-the calling Java language method, which in turn, Garbage collects the reference-the array when It is no longer used. The array can be explicitly freed with the following call.
(*env), releasebytearrayelements (env, JB, (Jbyte *) m, 0);
The last argument to the ReleaseByteArrayElements function above can has the following values:
- 0:updates to the array from within the C code is reflected in the Java language copy.
JNI_COMMIT: The Java language copy is updated, and the local is not jbyteArray freed.
- Jni_abort:changes is isn't copied back, but the is
jbyteArray freed. The value is used only if the array was obtained with a get mode of JNI_TRUE meaning the array is a copy.
Be careful with the last parameter, if 0, the memory that M points to is released. If M is pointing exactly to an array on a stack, this may cause a random memory error in release. Can be avoided by jni_commit.
The implementation code might look like this
+void
+kaffejni_releasebytearrayelements (jnienv* env UNUSED, Jbytearray arr, jbyte* Elems, Jint mode)
+{
+begin_exception_handling_void ();
+
+if (Elems! = Unhand_array ((harrayofbyte*) arr)->body) {
+switch (mode) {
+case Jni_commit:
+memcpy (Unhand_array (harrayofbyte*) arr)->body, Elems, Obj_length ((harrayofbyte*) arr) * sizeof (jbyte));
+break;
+case 0:
+memcpy (Unhand_array (harrayofbyte*) arr)->body, Elems, Obj_length ((harrayofbyte*) arr) * sizeof (jbyte));
+kfree (Elems);
+break;
+case Jni_abort:
+kfree (Elems);
+break;
+}
+}
+end_exception_handling ();
+}
Jni_commit forces the native array to is copied back to the original array in the Java virtual machine. JNI_ABORTfrees the memory allocated for the native array without copying back the new contents
[Android Pro] Beware of parameter problems in Releasebytearrayelements