Java本机访问代码错误:“无效的内存访问"; [英] Java Native Access code Error: "Invalid memory access"

查看:183
本文介绍了Java本机访问代码错误:“无效的内存访问";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以访问test.h和test.so文件:

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);

我现在正在编写JNA代码以调用上述方法.

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);
   }
}


在此代码位置运行JNATest时遇到以下异常:


While running the JNATest facing following exception at this code location:

TestCLibrary.INSTANCE.insert_data

TestCLibrary.INSTANCE.insert_data

线程"main"中的异常; java.lang.Error:无效的内存访问位于 com.sun.jna.Native.invokeInt(本机方法)位于 com.sun.jna.Function.invoke(Function.java:419)位于 com.sun.jna.Function.invoke(Function.java:354)位于 com.sun.jna.Library $ Handler.invoke(Library.java:244)在 com.test.apps.$ Proxy0.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) ....

如何避免此类错误? 以及如何在JNA中使用默认值初始化Pointer?

How to avoid such error ? and how can I initialize a Pointer in JNA with default value ?

推荐答案

执行以下步骤后,JVM崩溃问题得到解决:

The JVM Crash problem was solved after I performed following steps:

1)Native.setProtected(true);

1) Native.setProtected(true);

2)使用本机库释放内存 Native.free(Pointer.nativeValue(my_pointer_by_ref.getValue()));

2) Free up memory using Native library Native.free(Pointer.nativeValue(my_pointer_by_ref.getValue()));

3)使用以下CLI选项运行程序- -XX:-UseLoopPredicate -DLD_PRELOAD =/usr/lib/jvm/java-8-oracle/jre/lib/amd64/libjsig.so

3) running the program with following CLI options - -XX:-UseLoopPredicate -DLD_PRELOAD=/usr/lib/jvm/java-8-oracle/jre/lib/amd64/libjsig.so

这篇关于Java本机访问代码错误:“无效的内存访问";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆