Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

Java
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.

Java
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.

Java
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.

Java
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
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

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.
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900