Click here to Skip to main content
13,737,216 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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.
Posted 10-Nov-15 20:02pm
Comments
nv3 11-Nov-15 3:05am
   
I don't think that the error can be found by looking at the few sources you have posted in your question.
Richard MacCutchan 11-Nov-15 5:41am
   
It's fairly clear that you have a NULL pointer somewhere. You need to add some logic to collect more information or retry the application under the debugger.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
mayooran99 12-Nov-15 0:14am
   
I tried creating a new memory instance everytime using the following code. It still doesn't help.

Memory pointerMemory = new Memory(Pointer.SIZE);
useMemory(pointerMemory);
allocateMemory();
ensureAllocated();

The above lines were pasted in the constructor of ipj_action method.
mayooran99 12-Nov-15 2:02am
   
WTH?
OriginalGriff 12-Nov-15 3:31am
   
Just a moron posting drivel: his account is on it's way outa here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 12 Nov 2015
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100