alchemista
Programmer
Hello,
I'm having a hellacious time trying to get JNI working for some non-trivial method calls (as in methods that take structure parameters, not simple ints). At first I was using JNI to call into Ada functions that were exported as C. This worked for a couple of functions that took no parameters. However, when I tried to call functions that did take nontrivial struct parameters, I would get strange low-level crashes like stack and jmpbuf errors.
After failing to debug the error, we guessed that this was a difference in the Java and Ada runtimes/linkages. Since we wanted to port the Ada code to C anyway, we took some time to do that instead. However, now I'm getting similar errors with JNI and C.
Inside a JNI method, I'm extracting fields from a Java object parameter and populating a C struct with the values. The fields are all primitives. Then, I call a C method that takes a structure. The first call goes fine, that method then copies the structure into another local variable (all in C now), and then passes that local variable to another method. However, on that third method, whenever I try to make a system call, it segfaults. It will segfault in something like fflush() or memcpy(). We are sure this has to do with the JNI because we can call these methods with no problems from C.
Is there something here I'm missing? I'm aware of having to create local and global references to java objects if you're going to use them after the method returns, however in this case I'm copying all values into local variables anyways. Also, the JNI method crashes before it returns.
Here's some pseudocode of what I'm doing:
<jni.cpp>
void myJniMethod(jobject obj) {
myStruct a;
a.c = getIntField(obj);
a.d = getIntField(obj);
myCMethod(a);
}
<someC.c>
void myCMethod(myStruct m) {
otherStruct o;
o.someData = m;
someOtherC(o);
}
<someO.c>
void someOtherC(otherStruct o) {
otherStruct v;
printf("test"
fflush(); // CRASHES HERE
memcpy(&v, &o, sizeof(otherStruct)); // OR WOULD CRASH HERE IF I TOOK OUT THE FFLUSH
}
Any help would be greatly appreciated!
I'm having a hellacious time trying to get JNI working for some non-trivial method calls (as in methods that take structure parameters, not simple ints). At first I was using JNI to call into Ada functions that were exported as C. This worked for a couple of functions that took no parameters. However, when I tried to call functions that did take nontrivial struct parameters, I would get strange low-level crashes like stack and jmpbuf errors.
After failing to debug the error, we guessed that this was a difference in the Java and Ada runtimes/linkages. Since we wanted to port the Ada code to C anyway, we took some time to do that instead. However, now I'm getting similar errors with JNI and C.
Inside a JNI method, I'm extracting fields from a Java object parameter and populating a C struct with the values. The fields are all primitives. Then, I call a C method that takes a structure. The first call goes fine, that method then copies the structure into another local variable (all in C now), and then passes that local variable to another method. However, on that third method, whenever I try to make a system call, it segfaults. It will segfault in something like fflush() or memcpy(). We are sure this has to do with the JNI because we can call these methods with no problems from C.
Is there something here I'm missing? I'm aware of having to create local and global references to java objects if you're going to use them after the method returns, however in this case I'm copying all values into local variables anyways. Also, the JNI method crashes before it returns.
Here's some pseudocode of what I'm doing:
<jni.cpp>
void myJniMethod(jobject obj) {
myStruct a;
a.c = getIntField(obj);
a.d = getIntField(obj);
myCMethod(a);
}
<someC.c>
void myCMethod(myStruct m) {
otherStruct o;
o.someData = m;
someOtherC(o);
}
<someO.c>
void someOtherC(otherStruct o) {
otherStruct v;
printf("test"
fflush(); // CRASHES HERE
memcpy(&v, &o, sizeof(otherStruct)); // OR WOULD CRASH HERE IF I TOOK OUT THE FFLUSH
}
Any help would be greatly appreciated!