This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: cni converting String args[]


littlejohn@paranoici.org writes:

 >  I'm quite lost finding a proper way to convert String[] args to a
 > char** array. The code I did is:
 > 
 > jint A::init(JArray< ::java::lang::String *> *args) {
 > 
 >   int argc = 0;
 >   char* arguments[argc];
 > 
 >   if (args != NULL)
 >   {
 >     argc = args->length;
 >     arguments[argc];
 > 
 >     /*
 >      * convert the JArray<String*>*  in char**
 >      */
 >     ::java::lang::String** argsElements = elements(args);
 >     //args[0];
 > 
 >     for (int i = 0; i < argc; i++) {
 >       //arguments[i] = (char *) JvGetStringChars(argsElements[i]);
 >       arguments[i] = (char *) elements(argsElements[i]->getBytes());

If you want chars, you'll need an encoding -- elements returns a
jchar*.  Try JvGetStringUTFRegion.

 >       //arguments[i] = (char *) elements(argsElements[i]->toCharArray());
 >       printf("%s\n ", arguments[i]);
 >     }
 >   }
 >   return 0;
 > }
 > 
 >  Aside this method does nothing but the conversion, I think I'm
 > misusing the elements function. On the first iteration the printf code
 > goes ok, on the second it writes garbage of gets a SIGSEGV, because
 > the argsElements[1] points to argsElement[0]+4 (I checked with gdb).
 >  The commented out lines do not work either.
 > 
 >  What am I missing?

I'm not exactly sure, but I wonder if something is being GCd under
your feet.  Is everything here called from Java code, or do you have a
pointer to a Java type stashed in native code somewhere?

Andrew.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]