Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JNI crashes calling into C methods with structs

Status
Not open for further replies.

alchemista

Programmer
Aug 16, 2002
5
0
0
GB
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(&quot;test&quot;);
fflush(); // CRASHES HERE

memcpy(&v, &o, sizeof(otherStruct)); // OR WOULD CRASH HERE IF I TOOK OUT THE FFLUSH

}

Any help would be greatly appreciated!
 
Hi,
I'm not sure I can be much help, because JNI is hopeless at reporting errors. I would check immediately after your GetIntField calls to see if you've actually retrieved the values you expect. It could be that you've passed a null object at this stage (or something else). If not, then trace through your calls to see when things go wrong. It's
painfully hard work, but it's pretty much the only way to go. I've never had a problem in getting JNI to work (eventually :)) with very similar code to what you seem to be doing. But I ALWAYS end up peppering my native methods with couts and printfs until I'm sure it's working when I can #ifdef them out.

Graeme
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top