17 messages in net.java.dev.jna.usersRe: [jna-users] Array of Structures i...
FromSent OnAttachments
RWAD...@UP.COMJul 18, 2008 8:39 am 
Timothy WallJul 18, 2008 9:26 am 
RWAD...@UP.COMJul 18, 2008 9:46 am 
Timothy WallJul 18, 2008 10:18 am 
RWAD...@UP.COMJul 18, 2008 11:07 am 
RWAD...@UP.COMJul 18, 2008 11:19 am 
Timothy WallJul 18, 2008 11:35 am 
Timothy WallJul 18, 2008 11:40 am 
Timothy WallJul 18, 2008 11:44 am 
Timothy WallJul 18, 2008 11:46 am 
RWAD...@UP.COMJul 18, 2008 12:01 pm 
RWAD...@UP.COMJul 18, 2008 12:07 pm 
RWAD...@UP.COMJul 18, 2008 12:09 pm 
RWAD...@UP.COMJul 18, 2008 12:28 pm 
Timothy WallJul 18, 2008 12:46 pm 
RWAD...@UP.COMJul 18, 2008 12:55 pm 
RWAD...@UP.COMJul 18, 2008 1:16 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Array of Structures in a StructureActions...
From:RWAD...@UP.COM (RWAD@UP.COM)
Date:Jul 18, 2008 12:09:50 pm
List:net.java.dev.jna.users

Unfortunately, I can't print the structure after the write() (made must before the native function call), because the JVM crash causes us to not return from the native call.

_______________________________

Hardware /nm./: the part of the computer you can kick.

Timothy Wall <twal@dev.java.net> 07/18/2008 01:41 PM Please respond to use@jna.dev.java.net

To use@jna.dev.java.net cc

Subject Re: [jna-users] Array of Structures in a Structure

BTW, you should print the structure after calling Structure.write(); prior to that, inline structures may still have their originally allocated memory. Structure.write() is called automatically for you prior to a native function call.

On Jul 18, 2008, at 2:20 PM, RWAD@UP.COM wrote:

I removed my toString() override, and now I get this output. Can you tell whether the memory is allocated properly?

Size of ObjectPointAndOrientationStructure: 296 bytes Size of opo array: 592 ObjectPointAndOrientationStructure(allocated@0x376ae8 (592 bytes) (shared from allocated@0x376ae8 (592 bytes))) { ObjectPointStructure obj_pnt@0=ObjectPointStructure(allocated@0x323c6a8 (24 bytes)) { int obj_pnt_nbr@0=0 int fac_type_sys_nbr@4=0 int fac_enty_nbr@8=0 int rail_loca_nbr@c=0 byte obj_pnt_type_code[5]@10=[B@1a897a9 } int num_rpls@18=0 OrientationStructure ornt[3]@20=[Lcom.uprr.efm.nativ.struct.OrientationStructure;@17cec96 } ObjectPointAndOrientationStructure(allocated@0x376c10 (296 bytes) (shared from allocated@0x376c10 (296 bytes) (shared from allocated@0x376ae8 (592 bytes) (shared from allocated@0x376ae8 (592 bytes))))) { ObjectPointStructure obj_pnt@0=ObjectPointStructure(allocated@0x376c10 (296 bytes) (shared from allocated@0x376c10 (296 bytes) (shared from allocated@0x376c10 (296 bytes) (shared from allocated@0x376ae8 (592 bytes) (shared from allocated@0x376ae8 (592 bytes)))))) { int obj_pnt_nbr@0=0 int fac_type_sys_nbr@4=0 int fac_enty_nbr@8=0 int rail_loca_nbr@c=0 byte obj_pnt_type_code[5]@10=[B@90832e } int num_rpls@18=0 OrientationStructure ornt[3]@20=[Lcom.uprr.efm.nativ.struct.OrientationStructure;@1947496 } # # An unexpected error has been detected by HotSpot Virtual Machine: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x03cdebf1, pid=880, tid=1100 # # Java VM: Java HotSpot(TM) Client VM (1.5.0_04-b05 mixed mode, sharing) # Problematic frame: # C [efms-api.dll+0x2ebf1] # # An error report file with more information is saved as hs_err_pid880.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp # Connected to dev091 as dtns999 Entered efm111 PROPO array pointer was NOT null. Building log entry About to write log entry Wrote log entry Building log entry with PROPO data About to write PROPO data to log Wrote PROPO data to log Sizeof(Op_And_Ornt_Struct) = 296 bytes About to initialize SOP output array: 592 bytes starting at 1000E5A4.

_______________________________

Hardware /nm./: the part of the computer you can kick.

RWAD@UP.COM 07/18/2008 01:08 PM Please respond to use@jna.dev.java.net

To use@jna.dev.java.net cc Subject Re: [jna-users] Array of Structures in a Structure

I'm not clear how to tell that the structures are "using the proper shared memory," but here's the output. FYI, I made a small change to the Java structure definition based on your earlier feedback; new code follows the output below.

Size of ObjectPointAndOrientationStructure: 296 bytes Size of opo array: 592 OPOS [OPS [obj_pnt_nbr =0,fac_type_sys_nbr=0,fac_enty_nbr=0,rail_loca_nbr=0],numRpl=0, [Lcom.uprr.efm.nativ.struct.OrientationStructure;@1a897a9] OPOS [OPS [obj_pnt_nbr =0,fac_type_sys_nbr=0,fac_enty_nbr=0,rail_loca_nbr=0],numRpl=0, [Lcom.uprr.efm.nativ.struct.OrientationStructure;@17cec96] # # An unexpected error has been detected by HotSpot Virtual Machine: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x03cdebf1, pid=5080, tid=2040 # # Java VM: Java HotSpot(TM) Client VM (1.5.0_04-b05 mixed mode, sharing) # Problematic frame: # C [efms-api.dll+0x2ebf1] # # An error report file with more information is saved as hs_err_pid5080.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp # Connected to dev091 as dtns999 Entered efm111 PROPO array pointer was NOT null. Building log entry About to write log entry Wrote log entry Building log entry with PROPO data About to write PROPO data to log Wrote PROPO data to log Sizeof(Op_And_Ornt_Struct) = 296 bytes About to initialize SOP output array (592 bytes).

Current class definition:

package com.uprr.efm.nativ.struct;

//----------------------------------------------------------------------------------------------

public class ObjectPointAndOrientationStructure extends ApiStructure {

public ObjectPointStructure obj_pnt ; public int num_rpls ; public OrientationStructure[] ornt = new OrientationStructure[3];

//----------------------------------------------------------------------------------------------

@Override public String toString() {

final StringBuilder builder = new StringBuilder("OPOS");

add(builder, null , obj_pnt ); add(builder, "numRpl" , num_rpls ); add(builder, null , ornt );

return end(builder); } }

_______________________________

Hardware /nm./: the part of the computer you can kick.

Timothy Wall <twal@dev.java.net> 07/18/2008 12:19 PM Please respond to use@jna.dev.java.net

To use@jna.dev.java.net cc Subject Re: [jna-users] Array of Structures in a Structure

try Structure.toString and verify that the nested structures are using the proper shared memory; memory gets reallocated after your ctor when you do the "toArray", so you want to ensure that the nested structures' memory gets updated prior to entering your native code.

On Jul 18, 2008, at 12:46 PM, RWAD@UP.COM wrote:

This is getting more puzzling the longer I look at it. I had both the Java code & the C code output the size of the array that's being initialized, and they both agree that it's 592 bytes (red text below is from Java, blue is from C).

Size of ObjectPointAndOrientationStructure: 296 bytes Size of opo array: 592 # # An unexpected error has been detected by HotSpot Virtual Machine: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x03cdebf1, pid=5936, tid=3792 # # Java VM: Java HotSpot(TM) Client VM (1.5.0_04-b05 mixed mode, sharing) # Problematic frame: # C [efms-api.dll+0x2ebf1] # # An error report file with more information is saved as hs_err_pid5936.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp # Connected to dev091 as dtns999 Entered efm111 PROPO array pointer was NOT null. Building log entry About to write log entry Wrote log entry Building log entry with PROPO data About to write PROPO data to log Wrote PROPO data to log Sizeof(Op_And_Ornt_Struct) = 296 bytes About to initialize SOP output array (592 bytes).

This the offending C code. The print() before the memset succeeds, but we never see the output from the puts() call after the memset. Is it possible the array doesn't have ownership of its own memory?

printf("About to initialize SOP output array (%i bytes). \n", sizeof(Op_And_Ornt_Struct)*MAX_SOPS_AT_RL); memset(&(sop[0]),'\0', sizeof(Op_And_Ornt_Struct)*MAX_SOPS_AT_RL); puts("Initialized"); (doesn't get here)

_______________________________

Hardware /nm./: the part of the computer you can kick.

Timothy Wall <twal@dev.java.net> 07/18/2008 11:27 AM Please respond to use@jna.dev.java.net

To use@jna.dev.java.net cc Subject Re: [jna-users] Array of Structures in a Structure

On Jul 18, 2008, at 11:39 AM, RWAD@UP.COM wrote:

I'm having trouble properly initializing a C structure which contains an array of other structures. The C structure definition is:

typedef struct op_and_ornt_struct { Obj_Pnt_Struct obj_pnt; short int num_rpls; struct { Rail_Path_Loca_Struct rpl; Ropo_Struct ropo; double dist_from; } ornt[3]; } Op_And_Ornt_Struct;

____________________________________________________________

The Java code I'm using to emulate this structure is as follows:

package com.uprr.efm.nativ.struct; import java.util.List; import com.uprr.net.Orientation; import com.uprr.point.ObjectPointOrientation;

public class ObjectPointAndOrientationStructure extends Structure {

public ObjectPointStructure obj_pnt ; public int num_rpls ; public OrientationStructure[] orientations ;

You can initialize here with "orientations = new OrientationStructure[3];". JNA will auto-initialize the array as needed. You only need to initialize it if you need to initialize the data.

/** Default constructor. */ public ObjectPointAndOrientationStructure() { orientations = (OrientationStructure[])new OrientationStructure().toArray(3); } }

____________________________________________________________

I then create a 2-element array of these structures & pass it to a C function as follows:

final ObjectPointAndOrientationStructure opo = new ObjectPointAndOrientationStructure() ; final ObjectPointAndOrientationStructure[] opos =

(ObjectPointAndOrientationStructure[])opo.toArray(2);

myFunction(opos);

____________________________________________________________

The C function then tries to fill the 2-element "opos" array with zeroes, but crashes. Apparently the Java structure size differs from the C structure size.

That's a different problem than just initializing the array.

I'm guessing that may that the "orientations" array in the structure is not being inlined as intended, & that the Java constructor above is not creating it correctly. Does this make sense? If so, how do i instantiate the 3-element orientation array correctly so that it's inline inside of its parent structure?

The only way an array of structures would *not* be inlined is if they're declared to implement Structure.ByReference, in which case you'd get an array of structure pointers instead.

Check to see if the structure sizes match and if they don't use Structure.toString to figure out where they don't match.

You're using java "int" for native "short", which in most cases is incorrect. You probably want Java short instead.

---------------------------------------------------------------------

To unsubscribe, e-mail: user@jna.dev.java.net For additional commands, e-mail: user@jna.dev.java.net