JNA - 访问冲突,JVM 终止
我正在调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我开发一个使用 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.
尝试将 MyCallback 编写为 com.sun.jna.Callback 而不是 com.sun.jna.win32.StdCallLibrary.StdCallCallback :
问候,
伊曼纽尔·吉拉德
Try to write your MyCallback as an com.sun.jna.Callback instead as an com.sun.jna.win32.StdCallLibrary.StdCallCallback :
Regards,
Emmanuel Girard