Reputation: 2328
Here are the global initializers:
int width = 100;
int height = 100;
int cells = width * height;
int* pixels = (int*) malloc(sizeof(int) * cells);
int i;
for (i = 0; i < cells; i++) {
pixels[i] = 255;
}
Here is the code to copy the array back up to Java via JNI call:
void Java_com_example_app_setPixels(
JNIEnv *env, jobject obj, jintArray arr) {
// initializations, declarations, etc
jint *c_array;
jint i = 0;
// get a pointer to the array
c_array = (*env)->GetIntArrayElements(env, arr, NULL);
// do some exception checking
if (c_array == NULL) {
return; /* exception occurred */
}
for (i = 0; i < cells; i++) {
c_array[i] = (jint) pixels[i];
}
// release the memory so java can have it again
(*env)->ReleaseIntArrayElements(env, arr, c_array, 0);
}
This leads to a mostly filled Java array - however it stops about 80% of the way through.
However if I change:
c_array[i] = (jint) pixels[i];
to
c_array[i] = 255;
The modified Java array is filled.
Upvotes: 1
Views: 859
Reputation: 2328
As always it was my own fault. The array being allocated (pixels) and the array it was copied into were not of the same size.
Once fixed the array sizes everything worked correctly.
Upvotes: 1