为什么偶尔会抛出无效的内存访问异常? [英] Why is Invalid memory access exception thrown occasionally?

查看:121
本文介绍了为什么偶尔会抛出无效的内存访问异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个偶尔抛出InvalidMemoryAccessException的JNA结构。在不更改代码的情况下,当我运行它时,它有时会运行。有时会抛出异常,如下所示。



线程中的异常  main java.lang.Error:无效的内存访问
at com.sun.jna.Native.getInt(Native Method)
at com.sun.jna.Pointer.getInt(Pointer.java: 602
at com.sun.jna.Pointer.getValue(Pointer.java:< span class =code-digit> 390 )
at com.sun.jna.Structure.readField(Structure.java: 653
at com.sun.jna.Structure.read(Structure.java: 521
at com.sun.jna.Structure.autoRead( Structure.java: 1882
at com.sun.jna.Structure.conditionalAutoRead(Structure.java: 491
at com.sun.jna.Function.invoke(Function.java: 418





下面显示的是我在C中的结构。



 RFIDLIB_API ipj_error start(
ipj_iri_device * iri_device / * [in] * /
ipj_action action / * [in] * / );

typedef uint32_t ipj_action;





以下是我的Java实现。



< pre lang =java> public class ipj_action extends 结构{

public int ipj_action_value;

public ipj_action(){
setAlignType(Structure.ALIGN_NONE);
allocateMemory();
read();
}

@覆盖
protected 列表getFieldOrder (){
return Arrays.asList( ipj_action_value);

}

}





以下是我打电话的主要课程它。



  public   class  RFIDMain {

public rfidlib rlib;
public ipj_iri_device ipj_iri_device;
public ipj_action ipj_action;
public ipj_error errorStatus;

public static void main( String [] args){

RFIDMain r = new RFIDMain();


r.rlib =(rfidlib)Native.loadLibrary( rfidlib ,rfidlib。 class );
r.ipj_iri_device = new ipj_iri_device();
r.ipj_action = new ipj_action();
r.errorStatus = new ipj_error();
r.ipj_action.ipj_action_value = 0x1;

r.errorStatus = r.rlib.start(r.ipj_iri_device,r.ipj_action);

System.out.println(r.errorStatus);
}

}





我无法准确确定何时工作以及何时工作抛出一个例外。这个结构的内存分配有问题吗?请指教。

解决方案

'有时'的原因 是指向未初始化指针(空指针)的指针,可能会被不同的数据覆盖。 &NBSP;它的值通常会随着应用程序的每次执行而变化。 &NBSP;取决于它指向的位置取决于是否,或者更有可能,何时,你得到一个错误。



换句话说,错误可能来自什么应该是一个有效的内存地址,因为无效的内存地址会损坏它。




顺便说一句,使用调试器查找这个内容可能会很棘手,因为应用程序是通常在内存和发行版本中重新排列。


I have the a JNA structure which is throwing InvalidMemoryAccessException occasionally. Without changing the code,when I run it, it runs at times. At times it throw the exception as shown below.

Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.getInt(Native Method)
at com.sun.jna.Pointer.getInt(Pointer.java:602)
at com.sun.jna.Pointer.getValue(Pointer.java:390)
at com.sun.jna.Structure.readField(Structure.java:653)
at com.sun.jna.Structure.read(Structure.java:521)
at com.sun.jna.Structure.autoRead(Structure.java:1882)
at com.sun.jna.Structure.conditionalAutoRead(Structure.java:491)
at com.sun.jna.Function.invoke(Function.java:418)



Shown below is my structure in C.

RFIDLIB_API ipj_error start(
   ipj_iri_device* iri_device       /*[in]*/,
   ipj_action      action           /*[in]*/);

typedef uint32_t ipj_action;



Below is my Java implementation.

public class ipj_action extends Structure {

public int ipj_action_value;

public ipj_action() {
    setAlignType(Structure.ALIGN_NONE);
    allocateMemory();
    read();
}

@Override
protected List getFieldOrder() {
    return Arrays.asList("ipj_action_value");

}

}



Below is the main class from where I call it.

public class RFIDMain {

public  rfidlib rlib;
public  ipj_iri_device ipj_iri_device;
public  ipj_action ipj_action;
public  ipj_error errorStatus;

public static void main(String[] args) {

    RFIDMain r = new RFIDMain();


    r.rlib = (rfidlib) Native.loadLibrary("rfidlib", rfidlib.class);
    r.ipj_iri_device = new ipj_iri_device();
    r.ipj_action = new ipj_action();
    r.errorStatus = new ipj_error();
    r.ipj_action.ipj_action_value = 0x1;

    r.errorStatus = r.rlib.start(r.ipj_iri_device, r.ipj_action);

    System.out.println(r.errorStatus);
}

}



I can't figure exactly when it is working and when it is throwing an exception. Is this some problem with the memory allocation for the structure? Please advice.

解决方案

The reason for the 'sometimes'   is that a pointer, du to the un-initialized pointer (null pointer) is likely being over-written with varying data.   It's value typically changes with each execution of your application.   Depending upon where it's pointing will depend upon if, or more likely, when, you get an error.

In other words, the error can be from what should be a valid memory address because an invalid one corrupted it.


Finding this with a debugger, by the way, can be tricky as the application is often rearranged in memory vs. the release version.


这篇关于为什么偶尔会抛出无效的内存访问异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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