Java JNA:调用DLL,运行几次,然后报错“Invalid memory access”

问题描述 投票:0回答:0

DLL由Java创建,通过JNA调用Delphi,可以运行多次才会报错。是类型不对,还是DLL内存有问题什么的?

Java签名:

public interface DLL extends Library {

    DLL MTAWDll = Native.load("invoke", DLL.class);

    String invoke(String fileName, float a, float b, float c, float d,
                 float e, float f, float g, float h, float i,
                 float j, String k);

}

德尔福签名:

function invoke(filename: PAnsiChar; a, b, c, d, e, f, g, h, i, j: single; k: PAnsiChar): PAnsiChar; stdcall;

错误堆栈:

===================================9th time, call DLL===================================
===================================10th time, call DLLL===================================
2023-03-10 10:25:56.417 ERROR 12592 --- [-nio-94-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : > Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.Error: Invalid memory access] with root cause

java.lang.Error: Invalid memory access
  at com.sun.jna.Native.invokePointer(Native Method) ~[jna-5.5.0.jar:5.5.0 (b0)]
  at com.sun.jna.Function.invokePointer(Function.java:497) ~[jna-5.5.0.jar:5.5.0 (b0)]
  at com.sun.jna.Function.invokeString(Function.java:660) ~[jna-5.5.0.jar:5.5.0 (b0)]
  at com.sun.jna.Function.invoke(Function.java:434) ~[jna-5.5.0.jar:5.5.0 (b0)]
  at com.sun.jna.Function.invoke(Function.java:361) ~[jna-5.5.0.jar:5.5.0 (b0)]
  at com.sun.jna.Library$Handler.invoke(Library.java:265) ~[jna-5.5.0.jar:5.5.0 (b0)]
  at com.sun.proxy.$Proxy98.invoke(Unknown Source) ~[na:na]
  at com.xxx.xxx.dll.impl.DllImpl.invoke(DllImpl.java:40) ~[classes/:na]
  at com.xxx.xxx.dll.impl.DllImpl$$FastClassBySpringCGLIB$$92dce636.invoke(<generated>) ~[classes/:na]
  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.12.RELEASE.jar:5.1.12.RELEASE]
  at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:750) ~[spring-aop-5.1.12.RELEASE.jar:5.1.12.RELEASE]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.12.RELEASE.jar:5.1.12.RELEASE]
  at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:120) ~[spring-context-5.1.12.RELEASE.jar:5.1.12.RELEASE]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.12.RELEASE.jar:5.1.12.RELEASE]
  at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) ~[spring-aop-5.1.12.RELEASE.jar:5.1.12.RELEASE]
  at com.xxx.xxx.dll.impl.DllImpl$$EnhancerBySpringCGLIB$$c67e343.invoke(<generated>) ~[classes/:na]
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_341]
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_341]
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_341]
  at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_341]

JVM中的内存没有发现异常,也没有看到溢出。不知道是不是我查的数据有问题

方法调用:

public  class DllImpl extends DllManage implements DllProvider {

    public static AtomicInteger count = new AtomicInteger(0);
    @Override
    public synchronized ResponseEntity<String> invoke(String fileName, float a, float b,
                                                      float c, float d, float e, float f, float g, float h, float i, float j, String k) {
        System.setProperty("jna.encoding", "GBK");
        count.getAndIncrement();
        String result = null;
        System.out.println("==================================="+count+"th time, call DLL===================================");
        try {
            result = DLL.CCAWDll.invoke(fileName,
                    a,b,c,d,e,f,g,h,i,j,
                    k);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        System.gc();
        return new ResponseEntity<>(result, HttpStatus.OK);

    }
}
jna
© www.soinside.com 2019 - 2024. All rights reserved.