Java native ReleaseByteArrayElements() clearing buffer

geekoftheweek

Joined Oct 6, 2013
815
What I want to do is declare a buffer, pass it to the native code to modify, then use the changed buffer to update the app. This is an Android project that in the end will use another native library to do the actual work so this seems to be the best route to me.

In the main activity
Code:
byte the_buffer[];
the_buffer = new byte[1024];

...
writes the bytes out in a list to scroll through
which calls the native

Code:
JNIEXPORT jint JNICALL Java_com_example_myapp_mylibrary_ReadMessage(JNIEnv *pEnv, jobject pThis, jint nexiq_id, jbyteArray buffer) {
jboolean is_copy;
jbyte *buf = (char*)pEnv->GetByteArrayElements(buffer,&is_copy);
char char_buf[1024] = {0};

// test

int i;
for (i = 0; i < bytes_read; i++) {
char_buf[i] = (char)(rand() & 0xff);
}

pEnv->SetByteArrayRegion(buffer,0,i,(jbyte*)char_buf);
pEnv->ReleaseByteArrayElements(buffer, buf, 0);

}
If I leave in the ReleaseByteArrayElements() when it returns from native to write the buffer in a list the buffer seems to reset to all 0's.
If I comment out the ReleaseByteArrayElements() the buffer keeps the random numbers.

Is this right or did I miss something?

In the end instead of filling it with random numbers it will call a read function of another library and return that data.

geekoftheweek

Joined Oct 6, 2013
815
I spent a few more hours looking at code examples trying to find a definite answer, but still no luck. All seem to show sending data to the native calls, but nothing showing modifying the data to be used outside of the native call.

I kind of fell asleep and it run for about an hour before it crashed. More or less it just keeps looping and calling the native as fast as it can. From what I can tell the crash was more from the scroll view getting too big than any other sort of memory leaks that are associated with not calling the ReleaseByteArrayElements().

I have a semi decent grasp of Java, but the native stuff was a whole new learning experience... Thanks all for looking!!

xox

Joined Sep 8, 2017
678
What I want to do is declare a buffer, pass it to the native code to modify, then use the changed buffer to update the app. This is an Android project that in the end will use another native library to do the actual work so this seems to be the best route to me.

In the main activity
Code:
byte the_buffer[];
the_buffer = new byte[1024];

...
writes the bytes out in a list to scroll through
which calls the native

Code:
JNIEXPORT jint JNICALL Java_com_example_myapp_mylibrary_ReadMessage(JNIEnv *pEnv, jobject pThis, jint nexiq_id, jbyteArray buffer) {
jboolean is_copy;
jbyte *buf = (char*)pEnv->GetByteArrayElements(buffer,&is_copy);
char char_buf[1024] = {0};

// test

int i;
for (i = 0; i < bytes_read; i++) {
char_buf[i] = (char)(rand() & 0xff);
}

pEnv->SetByteArrayRegion(buffer,0,i,(jbyte*)char_buf);
pEnv->ReleaseByteArrayElements(buffer, buf, 0);

}
If I leave in the ReleaseByteArrayElements() when it returns from native to write the buffer in a list the buffer seems to reset to all 0's.
If I comment out the ReleaseByteArrayElements() the buffer keeps the random numbers.

Is this right or did I miss something?

In the end instead of filling it with random numbers it will call a read function of another library and return that data.
JNI is a very fickle beast. Lots of undocumented behavior relating to specific parameter states and what have you.

Try passing NULL in place of the is_copy parameter. And while you're at it, why not just write directly to the buffer instead of the double-pass?

Another thing. Rather than repeated subtractions you can simply use the modulus operator.

Code:
JNIEXPORT jint JNICALL Java_com_example_myapp_mylibrary_ReadMessage(JNIEnv *pEnv, jobject pThis, jint nexiq_id, jbyteArray buffer) {
jbyte *char_buf = (char*)pEnv->GetByteArrayElements(buffer, NULL);
int bytes_read = rand() % 1024;
int i;
for (i = 0; i < bytes_read; i++) {
char_buf[i] = (char)(rand() & 0xff);
}
pEnv->ReleaseByteArrayElements(buffer, char_buf, 0);
}

geekoftheweek

Joined Oct 6, 2013
815
Try passing NULL in place of the is_copy parameter. And while you're at it, why not just write directly to the buffer instead of the double-pass?

Another thing. Rather than repeated subtractions you can simply use the modulus operator.
Thanks for the tips. I do remember seeing the modulus somewhere, but lost it somewhere while digging around.

xox

Joined Sep 8, 2017
678
Thanks for the tips. I do remember seeing the modulus somewhere, but lost it somewhere while digging around.
You're welcome. Let us know how it worked out. It's been a while since I've had to work with JNI. Major pain in the arse.

geekoftheweek

Joined Oct 6, 2013
815
@xox I finally made my way back to this project. You put things on the right track after a couple tweaks. What I finally ended up with...

Code:
JNIEXPORT jint JNICALL Java_com_example_mytest_testLib_ReadMessage(JNIEnv *pEnv, jobject pThis, jint device_id, jbyteArray buffer) {
jbyte *buf = pEnv->GetByteArrayElements(buffer,nullptr);
}