[Android Pro] Beware of parameter problems in Releasebytearrayelements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.