9 messages in net.java.dev.jna.usersRe: [jna-users] Nested Structure Issue
FromSent OnAttachments
Peter SchwarzSep 22, 2008 2:26 pm 
Peter SchwarzSep 22, 2008 4:14 pm 
Timothy WallSep 22, 2008 7:24 pm 
Peter SchwarzSep 22, 2008 7:27 pm 
Timothy WallSep 22, 2008 7:37 pm 
Peter SchwarzSep 22, 2008 7:57 pm 
Timothy WallSep 22, 2008 9:05 pm 
Peter SchwarzSep 23, 2008 3:14 pm 
Timothy WallSep 23, 2008 4:54 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] Nested Structure IssueActions...
From:Timothy Wall (twal@dev.java.net)
Date:Sep 22, 2008 9:05:55 pm
List:net.java.dev.jna.users

On Sep 22, 2008, at 10:57 PM, Peter Schwarz wrote:

I can see how I wasn't being terribly clear there. Let me back up the thread to my original problem, and we'll forget about the Pointers for a moment. I'll try explaining it a little differently.

I have a struct, PyTypeObject (explicit info here: http://docs.python.org/ext/dnt-type-methods.html). I have no problem mapping the integer values, and the Function Callbacks (at least as far as I can tell - I haven't gotten far enough to test them). However, when I attempt to map either the String tp_name to char* tp_name, or the references to arrays of other structs, I get the memory access error listed in the first problem.

According to that link above, tp_name is not necessarily the first field in the structure. PyObject_VAR_HEAD expands to something, and you'll need to allocated space for it before tp_name. If you don't care about the contents, you can declare a nested structure and give it a fixed size, but you need to account for the space. Looks like about 3 pointers and two ints to me...

It doesn't seem so much like a nesting issue, as it is simply a problem with the mapping.

The error occurs when I do the following:

1) create a new PyTypeObject

2) set the value of tp_name (as well as a few other fields)

3) call a mapped version of PyModule_AddObject(), which accepts this object as a parameter

Here's where it blows up with the following:

Invalid memory access of location 00000001 rip=105fcee5

Does this help? In creating the struct, I haven't stepped across anything that seemed too out of the ordinary.

Cheers,

On 9/22/08 4:37 PM, "Timothy Wall" <twal@dev.java.net> wrote:

On Sep 22, 2008, at 10:27 PM, Peter Schwarz wrote:

The Field is one that I need to be able to set, as well. I get the memory access error if I'm setting the value of tp_name, if it's being read internally (by JNA).

I don't think I entirely understand what you're saying. You get the memory access error if you set tp_name, then subsequently cause a Structure.read() (either explicitly or by native function call)?

Structures are automatically written prior to a native call and read after a native call. Is the field always valid? If not, you need to turn off auto-read and synch it manually via Structure.readField().

When the field is set to a String from the Java side, JNA allocates memory for the native string and keeps a reference to it until the field value is changed.

If I do leave it as a Pointer in my stucture, how can I set this value, via the JNA API. It's not entirely clear.

Pointer.setString(0, <string>) and Pointer.getString(0), although you should probably figure out what's causing the fault when using String.

On 9/22/08 4:24 PM, "Timothy Wall" <twal@dev.java.net> wrote:

Strings are populated by reading from memory until a NUL byte is encountered. That doesn't happen with a Pointer.

If the pointer value doesn't point at a valid String, you don't want to read that field.

On Sep 22, 2008, at 7:14 PM, Peter Schwarz wrote:

Hi all (again),

Oddly enough, looking continuing to plug away at this further, I’m able to get my tests to pass if I use a Pointer instead of a String. The definition of the struct field is:

const char *tp_name;

Instead of using

public String tp_name;

which gives me the memory access error, I’m using

public Pointer tp_name;

Is there any particular reason this may be happening? Am I missing something simple?

Cheers,

Hi,

I’m currently working on adding mappings for structures (PyObject and PyTypeObject to be specific). The PyObject is pretty simple: a ref count and a pointer to its type. The PyTypeObject is much more complicated: Many arrays of structs, function pointers, etc.

I implemented the type object out as far as I need (the ability to define methods, members, etc), and I very quickly started getting errors. In order to find the culprit, I began by removing all the fields, and then adding them back one by one. I started with the int values, and my tests still passed. Once I added back the first String field, the tests fail in the native library with the following error:

Invalid memory access of location 00000001 rip=105fcee5

I’m not sure what might be causing this sort of error when defining Structure. Any ideas?

Cheers,