I m working on a project at the moment that requires me to receive a call in Java from a C library. Basically I call a C function that takes a function pointer, the C function then uses the function pointer as a callback. I m using JNA to pass a Java object (I ll call it the Callback object from now on) as the callback function. The callback object has a single method that receives a C structure (called Frame) that contains a 16 element byte array as well as other variables. I ve wrapped this structure in a Java class in the standard JNA way, like this:
class Frame extends Structure {
public short port;
public short flags;
public Pointer name // this is a pointer to the byte array
public int rateDivisor;
}
追索机制运作良好! 背心物体从C处收到一小块物体,但当我试图利用<代码>,即:getByteArray(0、16)从点子公司获得星体阵列时,该申请因暴力例外而坠毁。 然而,如果我用一阵列取代点子:
class Frame extends Structure {
public short port;
public short flags;
public byte[] name = new byte[16];
public int rateDivisor;
}
Then the code works fine! There s a good reason why I don t want to use this code however. Every time I call the function the returned Frame is actually the same object (it s just being reused) but the byte array is a new object. This callback function is being called many times a second which causes the garbage collector to goes crazy gobbling up thousands of temporary arrays. This project is performance critical so I really don t want any temporary objects in this part of the application.
My guess is that by using a byte array in the Frame class I m causing JNA to create a new copy of the C byte array. What I want to do is have a pointer to the C byte array therefore removing the need to copy the data, hence the experimentation with JNA Pointer.
My question is why can t I get the byte array from the Pointer? Any help would be much appreciated :)
(说明:使这种情况更加困难的是,我没有接触到《来源法》!)