java 内存位置访问无效,Java本机访问代码错误:“无效的内存访问";

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值