I'm working on an Android app for x86 that requires some integration with C. I've been using swig/JNI to do the trick, and things have been running smoothly for the most part. However, pointers have been giving me some errors.
My issue is that I am able to successfully reference variable addresses in the emulator (ARM) but on a device (x86), things do not go as well.
Using the example from this link, I discovered that the address of any allocated variable in C becomes NULL once this address passes over to Java. For example...
Swig-generated JNI:
SWIGEXPORT jlong JNICALL Java_exampleJNI_new_1intp(JNIEnv *jenv, jclass jcls) {
jlong jresult = 0 ;
int *result = 0 ;
(void)jenv;
(void)jcls;
result = (int *)new_intp();
LOGI("Result is %x", result);
*(int **)&jresult = result;
LOGI("JResult is %x", jresult);
return jresult;
}
Source file containing new_intp():
static int *new_intp() {
return (int *) calloc(1,sizeof(int));
}
I have print statements checking the value of the address as it originates in C and passes over to Java. In new_intp(), the new variable is allocated a good looking address, but once this value returns to JNI and gets cast as a jlong, it turns to NULL.
In other words, *(int **)&jresult = result;
causes jresult to be 0.
Why does this happen? Is there some particularity of x86 that disallows JNI to work with pointers? Or is it because I'm testing it on a physical device rather than an emulator?
Regards