From: Benjamin R. <ru...@ar...> - 2011-06-01 12:15:55
|
Hi everybody! I asked a question on SWIG-user mailing list and I think it is more appropriate to ask it on this mailing list. This is a copy of the message: Hi everybody! I have a problem of wrapping when I want use my wrapping generated by SWIG from C++ to Java. First I use a program in c++ to check if the code is good: //file.cpp #include <stdio.h> #include "GModel.h" #include "GModelFactory.h" #include "GVertex.h" int main(int argc, char** argv) { double x=1.0; double y=0.0; double z=0.0; double lc=0.1; GModel *m = new GModel(); GeoFactory *gFact=new GeoFactory(); GVertex *v=gFact->addVertex(m, x, y, z, lc); } I check this code works. Then I wrap with SWIG the code of c++ to java. The execution of wrapping works and there is no error. After I write an equivalent code in Java to do the same thing, and a problem appears: //Try.java public class Try{ static { try { System.loadLibrary("Wrap"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); System.exit(1); } } public static void main(final String[] args) { double x=1.0; double y=0.0; double z=0.0; double lc=0.1; GModel m = new GModel(); GeoFactory gFact=new GeoFactory(); GVertex v1=gFact.addVertex(m, x, y, z, lc);//the problem appears here } } This java code does not work. The error is: # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x00007f8814922724, pid=2064, tid=140222536300288 # # JRE version: 6.0_24-b07 # Java VM: Java HotSpot(TM) 64-Bit Server VM (19.1-b02 mixed mode linux-amd64 compressed oops) # Problematic frame: # C [libWrap.so+0x1e724] Java_WrapJNI_GeoFactory_1addVertex+0x4 # # An error report file with more information is saved as: # /home/benjamin/wrap_v1.2/wrap_v1.2/hs_err_pid2064.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. # Abandon To finish, I search the problem. I discover in cxx file generated by SWIG an interesting behavior. Indeed, we have: SWIGEXPORT jlong JNICALL Java_WrapJNI_new_1GeoFactory(JNIEnv *jenv, jclass jcls) { jlong jresult = 0 ; GeoFactory *result = 0 ; (void)jenv; (void)jcls; result = (GeoFactory *)new GeoFactory(); *(GeoFactory **)&jresult = result; return jresult; } The cast at the left of equal sign is deprecated and not recommended since gcc 3.5. If I modify this auto-generated file with: SWIGEXPORT jlong JNICALL Java_WrapJNI_new_1GeoFactory(JNIEnv *jenv, jclass jcls) { jlong jresult = 0 ; GeoFactory *result = 0 ; (void)jenv; (void)jcls; result = (GeoFactory *)new GeoFactory(); jresult = (jlong)result; //The only modification is here return jresult; } With this modification, the java program works. Does somebody know this problem? My OS is Ubuntu10.10 I use the SWIG 1.3.40, gcc 4.4 Thanks for your answers Benjamin JEANTY-RUARD |