• Post Reply Bookmark Topic Watch Topic
  • New Topic

JNI crashes calling into C methods with structs

 
Steven Ostrowski
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am sorry that my answer may not help you as i am myself a novice in JNI. But i am too facing similer problems.
I have written a simple java program that calls a C routine, which does nothing but prints a string.
I am soing it on Linux.
The java program properly calls the C routine through the JNI call. Even the statements that I have printed inside the C routine get printed . But while exiting from the C program the program Core dumps...
I tried a lot but couldn't understand the source of the problem.
Any help ?
Kaustubh.
 
Steven Ostrowski
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not exactly sure of what you're trying to do, some code posting may help. In case you don't know, if you intermix any strings between Java and C/C++, you need to conversions between the Unicode and UTF-8 formats. Also, in some cases you will need to explicitly de-allocate strings you have created. You can read up on strings in the JNI tutorial.
BTW - on my problem, it may turn out to not be a JNI-issue. I'm checking into it.
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply//
i am posting the source code below...
1. The c code is
-------------------------------------------
#include<stdio.h>
#include"NativeCall.h"
void Java_NativeCall_printSomething(JNIEnv * ptr,jobject obj)
{
printf("Inside the native C function..");
}
---------------------------------------------

2. The Java source code is
---------------------------------------------
public class NativeCall{
static{
System.loadLibrary("java-rtl");
}
public native void printSomething();
public static void main(String[] args){
try{
NativeCall nc = new NativeCall();
System.out.println("now calling the native method..");
nc.printSomething();
}catch(Exception e){
e.printStackTrace();
}
}
}
--------------------------------------------
Thanks,
Kaustubh
 
Steven Ostrowski
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks fine to me code-wise. Not sure why it would crash. I'd just re-verify that you built the shared library correctly.
You said it prints out the string but then crashes on the return? That should mean it is loading the shared library fine and finding the link fine. With such a simple function, I don't see what else could go wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!