JNA - 访问冲突,JVM 终止

发布于 2024-07-30 00:38:24 字数 1350 浏览 4 评论 0原文

我正在调用 DLL,并向其传递回调函数对象。 功能之一是简单打印。 然后,我有一个 100 次迭代的循环,只打印索引和循环后的一些打印。


这是 C 代码

extern "C" int Start(void* callback(CString))
{
   for(int j=0; j<100; j++)
    callback(AConvertToString(j));

   callback("in Start called from Java");
   callback("another line");
}

这是 Java 代码

public interface MyDll extends Library{
  MyDll INSTANCE = (MyDll) Native.loadLibrary("MyDll",MyDll.class);
     public interface MyCallback extends StdCallCallback {
            public boolean callback(String msg);
     }
     public int Start(MyCallback callback);
  }

//in main:
...
  MyDll myDll = (MyDll)MyDll.INSTANCE;
  myDll.Start(new MyDll.MyCallback() {
      public boolean callback(String msg) {
         System.out.println(msg);
          return true;          
      }
});

输出是数字 0..41(是的 41!!!不是 99),然后“在从 Java 调用的 Start 中”,然后是可怕的崩溃:

#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c809823, pid=468, tid=2636
#
# Java VM: Java HotSpot(TM) Client VM (10.0-b23 mixed mode, sharing windows-x86)
# Problematic frame:
# C  [kernel32.dll+0x9823]

我读了很多(这里是好吧)但我找不到问题。 我正在运行Java6的JRE。 我的机器有 1.5GB 内存。 该 DLL 不被任何其他进程使用(没有并发问题)。

谢谢, 阿兹瑞尔

I am calling a DLL with passing a callback functio object to it. One of the functions is simple print. I have then, a loop of 100 iterations, just printing the index and a few prints after the loop.


Here is the C code

extern "C" int Start(void* callback(CString))
{
   for(int j=0; j<100; j++)
    callback(AConvertToString(j));

   callback("in Start called from Java");
   callback("another line");
}

Here is the Java code

public interface MyDll extends Library{
  MyDll INSTANCE = (MyDll) Native.loadLibrary("MyDll",MyDll.class);
     public interface MyCallback extends StdCallCallback {
            public boolean callback(String msg);
     }
     public int Start(MyCallback callback);
  }

//in main:
...
  MyDll myDll = (MyDll)MyDll.INSTANCE;
  myDll.Start(new MyDll.MyCallback() {
      public boolean callback(String msg) {
         System.out.println(msg);
          return true;          
      }
});

The output is numbers 0..41 (YES 41!!! not 99) and then "in Start called from Java" followed by a horrible crash:

#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c809823, pid=468, tid=2636
#
# Java VM: Java HotSpot(TM) Client VM (10.0-b23 mixed mode, sharing windows-x86)
# Problematic frame:
# C  [kernel32.dll+0x9823]

I've read alot (here as well) yet I cannot find the problem.
I am running JRE of Java6. I have 1.5GB of memory on my machine. The DLL is not used by any other process (no concurrency issues).

Thanks,
Azriel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

养猫人 2024-08-06 00:38:24

在我开发一个使用 win32 dll 的电信应用程序的例子中,情况正好相反。

使用 com.sun.jna.Library 和 com.sun.jna.Callback 从回调调用返回时,虚拟机总是会崩溃。

更改为 com.sun.jna.win32.StdCallLibrary 和 com.sun.jna.win32.StdCallLibrary.StdCallCallback 后问题得到解决。

In my case developing a telecom application that uses a win32 dll it was the opposite case.

Using com.sun.jna.Library and com.sun.jna.Callback it always crashed the VM when returning from the callback call.

After changing to com.sun.jna.win32.StdCallLibrary and com.sun.jna.win32.StdCallLibrary.StdCallCallback the problem was solved.

梦旅人picnic 2024-08-06 00:38:24

尝试将 MyCallback 编写为 com.sun.jna.Callback 而不是 com.sun.jna.win32.StdCallLibrary.StdCallCallback :

public interface MyDll extends Library{
  MyDll INSTANCE = (MyDll) Native.loadLibrary("MyDll",MyDll.class);
     public interface MyCallback extends Callback {
            public boolean callback(String msg);
     }
     public int Start(MyCallback callback);
  }

问候,
伊曼纽尔·吉拉德

Try to write your MyCallback as an com.sun.jna.Callback instead as an com.sun.jna.win32.StdCallLibrary.StdCallCallback :

public interface MyDll extends Library{
  MyDll INSTANCE = (MyDll) Native.loadLibrary("MyDll",MyDll.class);
     public interface MyCallback extends Callback {
            public boolean callback(String msg);
     }
     public int Start(MyCallback callback);
  }

Regards,
Emmanuel Girard

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文