From: <si...@go...> - 2007-10-25 09:28:01
|
Hello list, I've got a problem generating a java wrapper for the following situation. Two structs, one of them containing a pointer (representing an array) to the other. I want to use if from java the following way. strA[] siArray = new strA[] {new strA()} ; container.setSi(siArray); strA[] tmpArray = container.getSi(); When I use the following swig-file, I get exactly the desired java API. /* The simplified Swig file */ %module Test %include "arrays_java.i" %inline %{ struct strA { int a; }; JAVA_ARRAYSOFCLASSES(strA) %apply strA[] {strA*}; struct Container { strA* si; }; %} But on the c++ side there is a little problem. Here is the generated function. SWIGEXPORT void JNICALL Java_TestJNI_Container_1si_1set(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jlongArray jarg2) { Container *arg1 = (Container *) 0 ; strA *arg2 = (strA *) 0 ; jlong *jarr2 ; jsize sz2 ; (void)jenv; (void)jcls; (void)jarg1_; arg1 = *(Container **)&jarg1; { int i; if (!jarg2) { SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array"); return ; } sz2 = jenv->GetArrayLength(jarg2); jarr2 = jenv->GetLongArrayElements(jarg2, 0); if (!jarr2) { return ; } arg2 = new strA[sz2]; if (!arg2) { SWIG_JavaThrowException(jenv, SWIG_JavaOutOfMemoryError, "array memory allocation failed"); return ; } for (i=0; i<sz2; i++) { arg2[i] = **(strA **)&jarr2[i]; } } if (arg1) (arg1)->si = arg2; { int i; for (i=0; i<sz2; i++) { jarr2[i] = 0; *(strA **)&jarr2[i] = &arg2[i]; } jenv->ReleaseLongArrayElements(jarg2, jarr2, 0); } delete [] arg2; } When I debug this function, all works correct until I reach the delete [] arg2 line. Here the array is deleted and therefore the pointer in the structure points to deleted memory. What am I doing wrong? Could this be a bug? Thank you Siggi |