来自JNI的EXCEPTION_ACCESS_VIOLATION(0xc0000005)JVM吗? [英] EXCEPTION_ACCESS_VIOLATION (0xc0000005) JVM from JNI?
问题描述
我在带有JNI的vc ++中编写了一些本机方法,可以从Java中进行访问.我的三种方法中的两种完全可以正常工作,没有任何问题.但是,当我在运行时调用它时,我的最后一种方法一直导致以下错误消息:
I wrote some native methods in a vc++ with JNI to be accessed from java. Two of my three methods work perfectly fine with no issues. My last method, however, has been causing the following error message when i call it during runtime:
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x61e06550, pid=3408, tid=4796
#
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) Client VM (21.0-b17 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0xa6550]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
这是我的本机方法的代码(在vc ++中):
Here is my native method's code (in vc++):
JNIEXPORT jcharArray JNICALL Java_jniusb_Main_receiveData (JNIEnv *env, jclass, jchar dataIndex)
{
DWORD BytesWritten = 0;
DWORD BytesRead = 0;
unsigned char OutputPacketBuffer[65];
unsigned char InputPacketBuffer[65];
static jcharArray ReturnPacketBuffer;
jchar temp[65];
//send 'receive data' command to the firmware (OutputPacketBuffer[1])
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
//retrieve data from firmware
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
for(int i=0;i<64;i++)
{
temp[i] = jchar(InputPacketBuffer[i+1]);
}
(*env).SetCharArrayRegion(ReturnPacketBuffer, 0, 64, temp);
return ReturnPacketBuffer;
}
我的Java代码看起来像这样(当然是经过删节的):
My Java code looks like this (abridged of course):
public static native char[] receiveData(char dataIndex);
public static void main(String[] args) {
char vid = 0x4d8;
char pid = 0x3f;
//check if read/write handles were retrieved
if(connectHid(vid, pid) == true)
{
System.out.println("connected!!!");
}
else
{
System.out.println("not connected...");
}
char[] test = new char[64];
char[] receivetest = new char[64];
char length = 0x03;
char dataIndex = 0x81;
test[0] = 0x80;
test[1] = 0x80;
sendData(test, length);
receivetest = receiveData(dataIndex);
就像我之前说过的那样,其他方法(即connect和senddata)也可以正常工作,但是我从receiveData方法中得到了错误.经过一些调试后,我发现注释掉该行后该错误消失了:
Like I said before, the other methods (i.e. connect and senddata) work fine and but I get the error from the receiveData method. After some debugging I discovered that the error goes away when I comment out the line:
(*env).SetCharArrayRegion(ReturnPacketBuffer, 0, 64, temp);
在我的本机代码中(当然,在这种情况下永远也不会返回数据...).我在这里做错了什么?
in my native code (of course the data never gets returned in this case...). What am I doing wrong here?
推荐答案
您是否错过了类似的东西
ReturnPacketBuffer = (*env)->NewCharArray(env, 64);
Aren't you missing something like
ReturnPacketBuffer = (*env)->NewCharArray(env, 64);
这篇关于来自JNI的EXCEPTION_ACCESS_VIOLATION(0xc0000005)JVM吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!