From: William S F. <ws...@fu...> - 2010-05-20 19:40:59
|
Lönnstedt Petter wrote: > Hi, > > I am performing a test by developing an example in Eclipse on Linux > (CentOS) in C++ (gcc-4.1.2-46) that I call from Java > (java-1.6.0-openjdk). The example works fine on Windows XP. > > In my test program in Java: When my class calls an implented virtual > method (declared pure virtual in the base class) I get the following error: > > > # SIGSEGV (0xb) at pc=0x0028f1f2, pid=31174, tid=3086662544 > # > # Java VM: OpenJDK Server VM (1.6.0-b09 mixed mode linux-x86) > # Problematic frame: > # C [libSwigTestSharedLib.so+0x11f2] > Java_exampleJNI_JavaCircle_1perimeter+0x12 > # > # An error report file with more information is saved as: > # /home/stpeln/swig/example_test/workspace/SwigTest/hs_err_pid31174.log > # > # If you would like to submit a bug report, please visit: > # _http://icedtea.classpath.org/bugzilla_ > # The crash happened outside the Java Virtual Machine in native code. > # See problematic frame for where to report the bug. > > There is no problem to call non-virtual methods that are implemented in > the base class as well as methods implemented in the class itself. > > Another problem is that when I declare static members, swig wraps this > in order but my Java code cannot even load the generated library (the > System.loadLibrary command fails). > > This is my C++ code: > > #ifndef __test_shape_h > #define __test_shape_h > > class JavaShape { > public: > JavaShape(); > virtual ~JavaShape(); > double x, y; > void move(double dx, double dy); > virtual double area() = 0; > virtual double perimeter() = 0; > }; > > #endif > > > > > #ifndef __test_circle_h > #define __test_circle_h > > #include "test_shape.h" > > class JavaCircle : public JavaShape { > private: > double radius; > public: > JavaCircle(double r); > // Implemented in this class: > virtual double area(); > virtual double perimeter(); > int myInt(); > }; > > #endif > > This is my Java code: > > JavaCircle myCircle = new JavaCircle(3); > // This works fine. > myCircle.move(1.0, 2.0); > myCircle.myInt(); > // This crashes the program: > myCircle.area(); > > > Would be great if anyone has an explanation for this! > > Thanks, > Not sure without getting exact details of what you are doing, like compiler options. If you modify Examples/java/class and send your mods here, I'll try and replicate. Otherwise use this as a guide for a correctly working compiler options etc under linux: ~/swig/trunk/Examples/java/class$ make make -f ../../Makefile CXXSRCS='example.cxx' SWIG='../../../preinst-swig' \ SWIGOPT='' TARGET='example' INTERFACE='example.i' java_cpp make[1]: Entering directory `/home/william/swig/trunk/Examples/java/class' ../../../preinst-swig -java -c++ example.i g++ -c -fpic example.cxx example_wrap.cxx -I"/usr/lib/jvm/java-6-sun-1.6.0.14/include" -I"/usr/lib/jvm/java-6-sun-1.6.0.14/include/linux" g++ -shared example.o example_wrap.o -o libexample.so make[1]: Leaving directory `/home/william/swig/trunk/Examples/java/class' javac *.java ~/swig/trunk/Examples/java/class$ env LD_LIBRARY_PATH=. java runme Creating some objects: Created circle Circle@1729854 Created square Square@6eb38a A total of 2 shapes were created Here is their current position: Circle = (20.0 30.0) Square = (-10.0 5.0) Here are some properties of the shapes: Circle@1729854 area = 314.1592653589793 perimeter = 62.83185307179586 Square@6eb38a area = 100.0 perimeter = 40.0 Guess I'll clean up now 0 shapes remain Goodbye ~/swig/trunk/Examples/java/class$ William |