3 messages in net.java.dev.jna.usersRe: [jna-users] question about String...
FromSent OnAttachments
Simon BASLEJun 18, 2007 10:47 am 
Timothy WallJun 18, 2007 11:23 am 
Simon BASLEJun 20, 2007 4:23 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] question about Strings and array pointersActions...
From:Timothy Wall (twal@dev.java.net)
Date:Jun 18, 2007 11:23:35 am
List:net.java.dev.jna.users

On Jun 18, 2007, at 1:47 PM, Simon BASLE wrote:

Ok I've just read your reply to my previous message and I've been struggling with it all the afternoon, Without success, so I'd like more details on it :)

But first I guess that I should myself give more details so here it goes : I am currently trying to use a C library called PVM, which allows me to make a virtual supercomputer using several computers accross a network. It's a distributed systems thing ;) The function that cause trouble is called spawn. It allows to spawn a process on a distant machine and link to the spawning one via PVM.

Here is the signature : int pvm_spawn(char *task, char **args, int spawnType, char *where, int nbTasks, int *tids); task is the string name of the executable file to spawn args is an array of strings which are the arguments passed to the spawned process. Last element must be NULL. where contains the hostname of the machine where to spawn if required by spawnType, NULL otherwise nbTasks is the number of tasks to spawn, tids is an array of size

= nbTasks which will receive the corresponding PVM ids

I've just recently added a mapping from String[] to char**; the string array is automatically converted into the required char* array format, including terminal NULL pointer. The download links all point to the latest binaries saved in SVN on the trunk; they were last updated about a week ago to include the String[] support.

when I try to map to String[] or byte[][] I receive an "unsupported array class" error so here is how I tried to map it : int pvm_spawn(String task, Memory spargs, int flag, String where, int ntask, int[] tids);

Even if you have to allocate memory yourself, you should typically use the Pointer type rather than the Memory type, as a matter of style.

and here is what I do to call the pvm_spawn method : ============================ int[] tids = null; String arg1 = "/home/balai/workspace/JnaTest/SecondApp.jar"; byte arg2 = '\0';

Memory ptr1 = new Memory(arg1.length()+1); ptr1.setString(0, arg1, false);

Memory ptr2 = new Memory(Byte.SIZE); ptr2.setByte(0, arg2);

Memory spargs = new Memory(ptr1.getSize()+ptr2.getSize()); spargs.setPointer(0, ptr1); spargs.setPointer(ptr1.getSize(), null);

You've essentially allocated a contiguous set of bytes, where each string follows the previous in memory. That's not the same thing as char**.

An array of strings (length one) in memory looks like this:

offset value [0x0000] [PPPPPPPP] -> "first string\0" [0x0004] [QQQQQQQQ] -> "second string\0" [0x0008] [00000000] (NULL)

So your "array" is a block of pointers of size Pointer.SIZE; for each offset of multiple Pointer.SIZE, you would do array.setPointer (offset, stringptr), where offset=index*Pointer.SIZE and stringptr is a Pointer to the memory holding your NUL-terminated string of char.

test.pvm_spawn("/home/balai/jnaJarLaunch", spargs, PvmCst.PvmTaskDefault, "noule", 1, tids);

Does the library behave properly when tids is null? if not, make sure it's not null.

But when I try to spawn a java task (with parameters) it tells me : # An unexpected error has been detected by Java Runtime Environment: # # SIGSEGV (0xb) at pc=0xb7e6bbfb, pid=13219, tid=3084737424 # # Java VM: Java HotSpot(TM) Client VM (1.6.0-b105 mixed mode, sharing) # Problematic frame: # C [libc.so.6+0x6dbfb] strlen+0xb

In the log the problem seems to be from Proxy$pvm_spawn or something like that... And the most strange of all : it runs correctly every now and then (but it's very rare, I'd say 3 times in 25 attemps maybe :[ )

This simply implies that you're using the contents of uninitialized memory or not the memory you intended to use.