I have access to test.h and test.so files:
test.h
int insert_data(void *location_data, uint64_t device_id, uint64_t event_id,
uint64_t motion_seq, uint64_t utc_time, uint32_t sensor_seq,
uint32_t device_seq, uint32_t tick_mark, uint16_t ble,
int movement, int path_id);
I am now writing JNA code to call the above method.
interface TestCLibrary extends Library {
TestCLibrary INSTANCE = (TestCLibrary) Native.loadLibrary(“test”, TestCLibrary);
int insert_data(Pointer test_data, long device_id, long event_id,
long motion_seq, long utc_time, int sensor_seq,
int device_seq, int tick_mark, short ble,
int movement, int path_id);
}
public class JNATest {
public static void main(String[] args) throws IOException{
Native.setProtected(true);
int path_id = 1, movement = 1, tick_mark = 1, device_seq = 1;
long utc_time = System.currentTimeMillis();
int sensor_seq = 100;
short ble = 25;
long motion_seq = 22222299;
long device_id = 111122333;
long event_id = 44445555;
Pointer test_data = Pointer.NULL;
TestCLibrary.INSTANCE.insert_data(test_data, device_id, event_id, motion_seq, utc_time, sensor_seq, device_seq, tick_mark, ble, movement, path_id);
}
}
While running the JNATest facing following exception at this code location:
TestCLibrary.INSTANCE.insert_data
Exception in thread "main" java.lang.Error: Invalid memory access at
com.sun.jna.Native.invokeInt(Native Method) at
com.sun.jna.Function.invoke(Function.java:419) at
com.sun.jna.Function.invoke(Function.java:354) at
com.sun.jna.Library$Handler.invoke(Library.java:244) at
com.test.apps.$Proxy0.insert_data(Unknown Source) ....
How to avoid such error ?
and how can I initialize a Pointer in JNA with default value ?
解决方案
The JVM Crash problem was solved after I performed following steps:
1) Native.setProtected(true);
2) Free up memory using Native library
Native.free(Pointer.nativeValue(my_pointer_by_ref.getValue()));
3) running the program with following CLI options -
-XX:-UseLoopPredicate -DLD_PRELOAD=/usr/lib/jvm/java-8-oracle/jre/lib/amd64/libjsig.so