Archived

This forum has been archived. Please start a new discussion on GitHub.

JNI | Java VM aborts on Ice::initialize

Hi,

I am trying to write a JNI wrapper for a library that uses ICE. It did not work and after
some debugging I found out that it seems to be ICE, to be specific Ice::initialize().

The Java VM crashes with a simple "unexpected exception" out of my control. I thought
about threads in general, but a simple pthreads example caused no problems. I tried
catching exceptions from Ice::initialize(), but none came. Some other ICE routines caused
no problems, either.

Did anyone have a similar problem with JNI and ICE? Is there anybody else who tried
something linke that?
Maybe it's just my machine or I missed something very, very important ...

I wrote some code which crashes for me. Thanks in advance for any help,

Christoph


File Demo.java
public class Demo {
        public static void main ( String[] _ ) {
                System.loadLibrary( "Demo" );
                jniDemo();
        }

        private static native void jniDemo ();
}

File Demo.cc
#include <Ice/Ice.h>
#include "Demo.h"

JNIEXPORT void JNICALL Java_Demo_jniDemo ( JNIEnv *, jclass ) {
        int a;
        Ice::initialize( a, 0 );
}

'best of' error message
An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : 11 occurred at PC=0x810DFE7
Function=[Unknown.]
Library=(N/A)

Demo.h was created with javah -jni -o Demo.h Demo
libDemo.so was build by g++ -shared -o libDemo.so -lIce

I am using Suse Linux, Kernel 2.4.20, GCC 3.2.3, Ice 1.3, Java 1.4.0

Comments

  • mes
    mes California
    Hi,

    I suspect the problem resides in the C++ code:
    JNIEXPORT void JNICALL Java_Demo_jniDemo ( JNIEnv *, jclass ) {
            int a;
            Ice::initialize( a, 0 );
    }
    
    The variable a is not initialized, therefore an arbitrary value could cause a segmentation fault. You should do something like this instead:
    JNIEXPORT void JNICALL Java_Demo_jniDemo ( JNIEnv *, jclass ) {
        int argc = 0;
        char* argv[] = { 0 };
        Ice::initialize( argc, argv );
    }
    
    I tried this and it worked correctly.

    Take care,
    - Mark
  • Hi,

    the code I posted was the shortest to reproduce my error. I tried that already, but after I altered the code again -- you never know ;) -- I still had the same problem. I asume it's something wrong with my machine, gcc or whatever.

    Just for the sake of completeness: Did you test the unpatched code? What was your testing environment?

    Thanks, Christoph
  • mes
    mes California
    Hi Christoph,

    Yes, I tried the unpatched code as well. It also worked, but after adding a line to print the value of a, I learned that it was being initialized to a negative value, therefore it wasn't triggering the error condition. Changing the code to initialize it to a positive value caused the same error message you received (signal 11, i.e., segfault).

    - Mark
  • Hi Mark,

    interesting. I now tried all three versions:
    If I initialize a as positive value, I get a simple "Aborted" from the VM, which makes sense as Ice::initialize() will try to read argv. Anyhow I get the very bad and ugly "unexpected exception" on a zero and negative values for a.

    Best regards, Christoph
  • mes
    mes California
    Can you post the exact code you're trying to use?

    BTW, I'm using RedHat Linux 9, gcc 3.2.2, Ice 1.3.0, JDK 1.4.2.

    - Mark
  • Actually it's the one you posted in your first reply.
    I tried it with argc as 1, -1 and 0. It get the error message I posted in extracts in my initial posting on -1 and 0. An 1 is replied by "Aborted" -- nothing else.

    If I fill argv with a value to match argc, I again get the 'big' message.
    JNIEXPORT void JNICALL Java_Demo_jniDemo ( JNIEnv *, jclass ) {
            int argc = 1;
            char* argv[] = { "a" };
            Ice::initialize( argc, argv );
    }
    
    Christoph
  • marc
    marc Florida
    Note that you must null-terminate argv:

    char* argv[] = { "a", 0 };
  • mes
    mes California
    Christoph,

    Are any core files created? If so, do the stack traces provide any helpful information?

    I've tried to reproduce the crash you describe, but so far I haven't been able to.

    - Mark
  • Hi,

    unfortunately there is no stack trace. The error message I posted in my initial posting is followed by a library listing, nothing else.
    As I mentioned before I am using a library which uses Ice itself. I am sure, Ice::initalize() is used correctly in there.

    Anyway (I think) I found the solution for my problem. I updated to Java 1.4.2_04 as this was a significant difference between your and my environment. It works now, I am able to call remote functions through Ice!

    If you want to, you should be able to reproduce the crash with Java 1.4.0.

    Thanks again for your help,

    Christoph