4

first question asked, so I'll get straight to it.

I've got some C code that will be interfacing with Java; i did my homework on JNI and the topic. Here's the code:

C-part:

#include <stdio.h>
#include <jni.h>
#include <string.h>

#define CLASSPATH "-Djava.class.path=/scratch/workareas/JTest/Java/" //folder which contains .class files

#define DEBUG 0

JNIEnv* create_vm(JavaVM ** jvm)
{

    JNIEnv *env;
    JavaVMInitArgs vm_args;
    JavaVMOption options;
    strcpy(options.optionString, CLASSPATH); //fix for options.optionString = CLASSPATH;
    if (DEBUG) printf("optionString = %s\n", options.optionString); 
    vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
    vm_args.nOptions = 1;
    vm_args.options = &options;
    vm_args.ignoreUnrecognized = 0;

    int ret = JNI_CreateJavaVM(jvm, (void**) &env, &vm_args);
    if (ret < 0) printf("\n<<<<< Unable to Launch JVM >>>>>\n");
    return env;
}

int main(int argc, char* argv[])
{
    JNIEnv* env;
    JavaVM* jvm;
    printf("Creating JVM....");
    env = create_vm(&jvm);
    printf(" done! [env = %p\tjvm = %p]\n", env, jvm);
    if (env == NULL) return 1;

    jclass myClass = NULL;
    jmethodID dispatchMessage = NULL;
    int counter = 0;

    //Obtaining Class
    myClass = (*env)->FindClass(env, "EventHandler");
    printf("FindClass done! [%p]\n", myClass);

    //Obtaining Method ID
    if (myClass != NULL)
        dispatchMessage = (*env)->GetStaticMethodID(env, myClass, "dispatchEvent", "(I)V");
    else
        printf("Unable to find the requested class\n");

    printf("Calling dispatchEvent() [%p] ...\n", dispatchMessage);
    if (dispatchMessage != NULL)
    {
//      jstring newMessage = (*env)->NewStringUTF(env, "Test call::Called from C\n");
        for(counter = 0; counter < 10; counter ++)
        {
            jint newMessage = counter;
            (*env)->CallStaticVoidMethod(env, myClass, dispatchMessage, newMessage);
        }
    } printf("dispatchMessage() done!\n");

    //Release resources.
    printf("Releasing resources...");
    (*jvm)->DestroyJavaVM(jvm);
    printf(" done! Exiting.\n");
    return 0;
}

Java part:

public class EventHandler
{
    public static final int EVENT_CODE_E1 = 1;
    public static final int EVENT_CODE_E2 = 2;
    //...
    public static final int EVENT_CODE_E10 = 10;

    private static EventHandler instance = new EventHandler();


    public EventHandler()
    {
        //TODO: create object here
    }

    public static EventHandler getInstance()
    {
        if(instance == null)
            instance = new EventHandler();
        return instance;
    }

    public static void dispatchEvent(int eventCode)
    {
        switch(eventCode)
        {
            case EVENT_CODE_E1:
                System.out.println("Event 1 firing!");
                getInstance().onEventE1();
                break;
            case EVENT_CODE_E2:
                System.out.println("Event 2 just fired!");
                break;
            //case
            default:
                System.out.println("Unknown event with ID: "+eventCode+" triggered!");
                break;
        }
    }

    private void onEventE1()
    {
        System.out.println("heyoooo");
    }
}

Now here comes the question: after performing the call 10 times, this is what I get as output:

Creating JVM.... done! [env = 0x80e6d20 jvm = 0x177e6a4]
FindClass done! [0x80e7c78]
Calling dispatchEvent() [0x90b3fe8c] ...
Unknown event with ID: 0 triggered!
Event 1 firing!
heyoooo
Event 2 just fired!
Unknown event with ID: 3 triggered!
Unknown event with ID: 4 triggered!
Unknown event with ID: 5 triggered!
Unknown event with ID: 6 triggered!
Unknown event with ID: 7 triggered!
Unknown event with ID: 8 triggered!
Unknown event with ID: 9 triggered!
dispatchMessage() done!
Releasing resources... done! Exiting.
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x632e6176, pid=14752, tid=3077634256
#
# JRE version: 6.0_26-b03
# Java VM: Java HotSpot(TM) Server VM (20.1-b02 mixed mode linux-x86 )
# Problematic frame:
# C  0x632e6176
[error occurred during error reporting (printing problematic frame), id 0xb]

# An error report file with more information is saved as:
# /scratch/workareas/JTest/hs_err_pid14752.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#
Aborted

I've got no idea what causes this crash. I've looked through the log and got nothing meaningful. Anyone cares to help please? :)

EDIT: I've also attached the log in case someone spots something interesting. I tried cross-referencing it with objdump of JNITest and tried using addr2line as well, it gave me nothing good. A GDB session was also uneffective at determining the root cause.

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x632e6176, pid=14752, tid=3077634256
#
# JRE version: 6.0_26-b03
# Java VM: Java HotSpot(TM) Server VM (20.1-b02 mixed mode linux-x86 )
# Problematic frame:
# C  0x632e6176
[error occurred during error reporting (printing problematic frame), id 0xb]

# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

---------------  T H R E A D  ---------------

Current thread is native thread

siginfo:si_signo=SIGSEGV: si_errno=0, si_code=1 (SEGV_MAPERR), si_addr=0x632e6176

Registers:
EAX=0x00000000, EBX=0x0063dff4, ECX=0x0063e4e0, EDX=0x0063f360
ESP=0xbfd2f380, EBP=0x616a442d, ESI=0x00000000, EDI=0x00000000
EIP=0x632e6176, EFLAGS=0x00210246, CR2=0x632e6176

Top of Stack: (sp=0xbfd2f380)
0xbfd2f380:   7373616c 7461702e 732f3d68 74617263
0xbfd2f390:   772f6863 616b726f 73616572 65544a2f
0xbfd2f3a0:   4a2f7473 2f617661 00f7c100 00f8bad0
0xbfd2f3b0:   b77108d0 0063dff4 00000000 00000000
0xbfd2f3c0:   bfd2f3f8 2f09115f 15974820 00000000
0xbfd2f3d0:   00000000 00000000 00000001 08048400
0xbfd2f3e0:   00000000 00f81d90 004fbc0b 00f8aff4
0xbfd2f3f0:   00000001 08048400 00000000 08048421 

Instructions: (pc=0x632e6176)
0x632e6156:   
[error occurred during error reporting (printing registers, top of stack, instructions near pc), id 0xb]

Register to memory mapping:

EAX=0x00000000 is an unknown value
EBX=0x0063dff4: <offset 0x158ff4> in /lib/libc.so.6 at 0x004e5000
ECX=0x0063e4e0: _IO_2_1_stdout_+0 in /lib/libc.so.6 at 0x004e5000
EDX=0x0063f360: <offset 0x15a360> in /lib/libc.so.6 at 0x004e5000
ESP=0xbfd2f380 is an unknown value
EBP=0x616a442d is an unknown value
ESI=0x00000000 is an unknown value
EDI=0x00000000 is an unknown value


Stack: [0xbfce0000,0xbfd30000],  sp=0xbfd2f380,  free space=316k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  0x632e6176
[error occurred during error reporting (printing native stack), id 0xb]


---------------  P R O C E S S  ---------------

VM state:at safepoint (shutting down)

VM Mutex/Monitor currently owned by a thread:  ([mutex/lock_event])
[0x080e4aa0] Threads_lock - owner thread: 0x90b72000

Heap
 PSYoungGen      total 9408K, used 322K [0xa9e40000, 0xaa8c0000, 0xb4640000)
  eden space 8064K, 4% used [0xa9e40000,0xa9e90b48,0xaa620000)
  from space 1344K, 0% used [0xaa770000,0xaa770000,0xaa8c0000)
  to   space 1344K, 0% used [0xaa620000,0xaa620000,0xaa770000)
 PSOldGen        total 21504K, used 0K [0x94e40000, 0x96340000, 0xa9e40000)
  object space 21504K, 0% used [0x94e40000,0x94e40000,0x96340000)
 PSPermGen       total 16384K, used 1751K [0x90e40000, 0x91e40000, 0x94e40000)
  object space 16384K, 10% used [0x90e40000,0x90ff5f40,0x91e40000)

Code Cache  [0xb470f000, 0xb494f000, 0xb770f000)
 total_blobs=90 nmethods=0 adapters=58 free_code_cache=50039104 largest_free_block=0

Dynamic libraries:
00110000-00133000 r-xp 00000000 08:06 1710021    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/libjava.so
00133000-00135000 rw-p 00023000 08:06 1710021    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/libjava.so
00135000-00148000 r-xp 00000000 08:06 1845308    /lib/libnsl-2.12.1.so
00148000-00149000 r--p 00012000 08:06 1845308    /lib/libnsl-2.12.1.so
00149000-0014a000 rw-p 00013000 08:06 1845308    /lib/libnsl-2.12.1.so
0014a000-0014c000 rw-p 00000000 00:00 0 
00154000-0015a000 r-xp 00000000 08:06 1845309    /lib/libnss_compat-2.12.1.so
0015a000-0015b000 r--p 00006000 08:06 1845309    /lib/libnss_compat-2.12.1.so
0015b000-0015c000 rw-p 00007000 08:06 1845309    /lib/libnss_compat-2.12.1.so
0015c000-00166000 r-xp 00000000 08:06 1845311    /lib/libnss_files-2.12.1.so
00166000-00167000 r--p 00009000 08:06 1845311    /lib/libnss_files-2.12.1.so
00167000-00168000 rw-p 0000a000 08:06 1845311    /lib/libnss_files-2.12.1.so
0019d000-001c1000 r-xp 00000000 08:06 1845306    /lib/libm-2.12.1.so
001c1000-001c2000 r--p 00023000 08:06 1845306    /lib/libm-2.12.1.so
001c2000-001c3000 rw-p 00024000 08:06 1845306    /lib/libm-2.12.1.so
001c3000-001c6000 ---p 00000000 00:00 0 
001c6000-00214000 rwxp 00000000 00:00 0 
00243000-00244000 ---p 00000000 00:00 0 
00244000-002c4000 rwxp 00000000 00:00 0 
002c4000-002c7000 ---p 00000000 00:00 0 
002c7000-00315000 rwxp 00000000 00:00 0 
0032d000-00342000 r-xp 00000000 08:06 1845316    /lib/libpthread-2.12.1.so
00342000-00343000 ---p 00015000 08:06 1845316    /lib/libpthread-2.12.1.so
00343000-00344000 r--p 00015000 08:06 1845316    /lib/libpthread-2.12.1.so
00344000-00345000 rw-p 00016000 08:06 1845316    /lib/libpthread-2.12.1.so
00345000-00347000 rw-p 00000000 00:00 0 
00347000-0034a000 ---p 00000000 00:00 0 
0034a000-00398000 rwxp 00000000 00:00 0 
003ba000-003c9000 r-xp 00000000 08:06 1710024    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/libzip.so
003c9000-003cb000 rw-p 0000e000 08:06 1710024    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/libzip.so
003cb000-003ce000 ---p 00000000 00:00 0 
003ce000-0044c000 rwxp 00000000 00:00 0 
0044c000-0044f000 ---p 00000000 00:00 0 
0044f000-004cd000 rwxp 00000000 00:00 0 
004e5000-0063c000 r-xp 00000000 08:06 1845302    /lib/libc-2.12.1.so
0063c000-0063e000 r--p 00157000 08:06 1845302    /lib/libc-2.12.1.so
0063e000-0063f000 rw-p 00159000 08:06 1845302    /lib/libc-2.12.1.so
0063f000-00642000 rw-p 00000000 00:00 0 
00642000-00643000 ---p 00000000 00:00 0 
00643000-006c3000 rwxp 00000000 00:00 0 
0077a000-0077c000 r-xp 00000000 08:06 1845305    /lib/libdl-2.12.1.so
0077c000-0077d000 r--p 00001000 08:06 1845305    /lib/libdl-2.12.1.so
0077d000-0077e000 rw-p 00002000 08:06 1845305    /lib/libdl-2.12.1.so
008ec000-008ed000 ---p 00000000 00:00 0 
008ed000-0096d000 rwxp 00000000 00:00 0 
0098b000-0098c000 r-xp 00000000 00:00 0          [vdso]
00991000-00992000 ---p 00000000 00:00 0 
00992000-00a12000 rwxp 00000000 00:00 0 
00a50000-00a57000 r-xp 00000000 08:06 1845318    /lib/librt-2.12.1.so
00a57000-00a58000 r--p 00006000 08:06 1845318    /lib/librt-2.12.1.so
00a58000-00a59000 rw-p 00007000 08:06 1845318    /lib/librt-2.12.1.so
00c40000-00c49000 r-xp 00000000 08:06 1845313    /lib/libnss_nis-2.12.1.so
00c49000-00c4a000 r--p 00008000 08:06 1845313    /lib/libnss_nis-2.12.1.so
00c4a000-00c4b000 rw-p 00009000 08:06 1845313    /lib/libnss_nis-2.12.1.so
00c76000-00c81000 r-xp 00000000 08:06 1710020    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/libverify.so
00c81000-00c82000 rw-p 0000b000 08:06 1710020    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/libverify.so
00e31000-00e34000 ---p 00000000 00:00 0 
00e34000-00e82000 rwxp 00000000 00:00 0 
00f6e000-00f8a000 r-xp 00000000 08:06 1845299    /lib/ld-2.12.1.so
00f8a000-00f8b000 r--p 0001b000 08:06 1845299    /lib/ld-2.12.1.so
00f8b000-00f8c000 rw-p 0001c000 08:06 1845299    /lib/ld-2.12.1.so
00f8c000-0173e000 r-xp 00000000 08:06 1710009    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/server/libjvm.so
0173e000-01792000 rw-p 007b1000 08:06 1710009    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/server/libjvm.so
01792000-01bb0000 rw-p 00000000 00:00 0 
08048000-08049000 r-xp 00000000 08:11 3670021    /scratch/workareas/JTest/JNItest
08049000-0804a000 r--p 00000000 08:11 3670021    /scratch/workareas/JTest/JNItest
0804a000-0804b000 rw-p 00001000 08:11 3670021    /scratch/workareas/JTest/JNItest
080e2000-08103000 rw-p 00000000 00:00 0          [heap]
90734000-90934000 r--p 00000000 08:06 919903     /usr/lib/locale/locale-archive
90934000-90968000 rw-p 00000000 00:00 0 
90968000-90b00000 r--s 03029000 08:06 1583846    /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/rt.jar
90b00000-90ba1000 rw-p 00000000 00:00 0 
90ba1000-90c00000 ---p 00000000 00:00 0 
90c5b000-90c63000 rw-p 00000000 00:00 0 
90c63000-90c7b000 rw-p 00000000 00:00 0 
90c7b000-90c86000 rw-p 00000000 00:00 0 
90c86000-90d23000 rw-p 00000000 00:00 0 
90d23000-90d2b000 rw-p 00000000 00:00 0 
90d2b000-90d43000 rw-p 00000000 00:00 0 
90d43000-90d4e000 rw-p 00000000 00:00 0 
90d4e000-90deb000 rw-p 00000000 00:00 0 
90deb000-90df1000 rw-p 00000000 00:00 0 
90df1000-90e3f000 rw-p 00000000 00:00 0 
90e3f000-91e40000 rw-p 00000000 00:00 0 
91e40000-94e40000 rw-p 00000000 00:00 0 
94e40000-96340000 rw-p 00000000 00:00 0 
96340000-a9e40000 rw-p 00000000 00:00 0 
a9e40000-aa8c0000 rw-p 00000000 00:00 0 
aa8c0000-b4640000 rw-p 00000000 00:00 0 
b464f000-b4658000 rw-p 00000000 00:00 0 
b4658000-b470f000 rw-p 00000000 00:00 0 
b470f000-b494f000 rwxp 00000000 00:00 0 
b494f000-b770f000 rw-p 00000000 00:00 0 
b770f000-b7711000 rw-p 00000000 00:00 0 
b771a000-b771b000 r--p 002a1000 08:06 919903     /usr/lib/locale/locale-archive
b771b000-b7723000 rw-s 00000000 08:06 1181409    /tmp/hsperfdata_rtrk/14752 (deleted)
b7723000-b7724000 rw-p 00000000 00:00 0 
b7724000-b7725000 ---p 00000000 00:00 0 
b7725000-b7728000 rw-p 00000000 00:00 0 
bfce4000-bfd30000 rwxp 00000000 00:00 0          [stack]
bfd30000-bfd32000 rw-p 00000000 00:00 0 

VM Arguments:
java_command: <unknown>
Launcher Type: generic

Environment Variables:
PATH=<snip, it's not the path>
USERNAME=rtrk
LD_LIBRARY_PATH=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/server:/home/rtrk/workspace/UPnP/bin-pc/usr/local/lib:
SHELL=/bin/bash
DISPLAY=:0.0

Signal Handlers:
SIGSEGV: [libjvm.so+0x725510], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGBUS: [libjvm.so+0x725510], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGFPE: [libjvm.so+0x5dff20], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGPIPE: [libjvm.so+0x5dff20], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGXFSZ: [libjvm.so+0x5dff20], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGILL: [libjvm.so+0x5dff20], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGUSR1: SIG_DFL, sa_mask[0]=0x00000000, sa_flags=0x00000000
SIGUSR2: [libjvm.so+0x5e3160], sa_mask[0]=0x00000000, sa_flags=0x10000004
SIGHUP: [libjvm.so+0x5e2d40], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGINT: [libjvm.so+0x5e2d40], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGTERM: [libjvm.so+0x5e2d40], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004
SIGQUIT: [libjvm.so+0x5e2d40], sa_mask[0]=0x7ffbfeff, sa_flags=0x10000004


---------------  S Y S T E M  ---------------

OS:squeeze/sid

uname:Linux 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686
libc:glibc 2.12.1 NPTL 2.12.1 
rlimit: STACK 8192k, CORE 0k, NPROC infinity, NOFILE 1024, AS infinity
load average:0.02 0.42 1.02

/proc/meminfo:
MemTotal:        2060876 kB
MemFree:           78596 kB
Buffers:          231004 kB
Cached:          1029676 kB
SwapCached:        42504 kB
Active:           711912 kB
Inactive:        1193204 kB
Active(anon):     399420 kB
Inactive(anon):   251192 kB
Active(file):     312492 kB
Inactive(file):   942012 kB
Unevictable:           0 kB
Mlocked:               0 kB
HighTotal:       1187784 kB
HighFree:          18836 kB
LowTotal:         873092 kB
LowFree:           59760 kB
SwapTotal:       1998844 kB
SwapFree:        1841240 kB
Dirty:               232 kB
Writeback:             0 kB
AnonPages:        613140 kB
Mapped:            47840 kB
Shmem:              6164 kB
Slab:              54384 kB
SReclaimable:      41476 kB
SUnreclaim:        12908 kB
KernelStack:        3072 kB
PageTables:         7384 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     3029280 kB
Committed_AS:    1775172 kB
VmallocTotal:     122880 kB
VmallocUsed:        7284 kB
VmallocChunk:     108952 kB
HardwareCorrupted:     0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       4096 kB
DirectMap4k:       16376 kB
DirectMap4M:      892928 kB


CPU:total 2 (1 cores per cpu, 1 threads per core) family 6 model 42 stepping 7, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt

/proc/cpuinfo:
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 42
model name  : Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz
stepping    : 7
cpu MHz     : 3093.040
cache size  : 6144 KB
fdiv_bug    : no
hlt_bug     : no
f00f_bug    : no
coma_bug    : no
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm ida arat pln pts
bogomips    : 6186.08
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 42
model name  : Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz
stepping    : 7
cpu MHz     : 3093.040
cache size  : 6144 KB
fdiv_bug    : no
hlt_bug     : no
f00f_bug    : no
coma_bug    : no
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm ida arat pln pts
bogomips    : 6186.08
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:



Memory: 4k page, physical 2060876k(78580k free), swap 1998844k(1841240k free)

vm_info: Java HotSpot(TM) Server VM (20.1-b02) for linux-x86 JRE (1.6.0_26-b03), built on May  4 2011 01:04:10 by "java_re" with gcc 3.2.1-7a (J2SE release)

time: Fri Dec 16 10:58:12 2011
elapsed time: 3 seconds

Thanks for the effort everyone :)

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Shark
  • 6,513
  • 3
  • 28
  • 50
  • /scratch/workareas/JTest/hs_err_pid14752.log what does this have in it? – Sid Malani Dec 16 '11 at 10:20
  • I've edited the opening post with an attached log. The error is supposedly somewhere in C, altho I don't really see where. The calls return OK as well so I'm not sure how this crash will affect this later - that's why I want to clear this issue before I start finishing the EventHandler. – Shark Dec 16 '11 at 10:40
  • It also might be worthy to mention that this exact same behaviour displays with both Sun's JRE and openjdk as well. I pretty much ran out of options which is why i'm asking. – Shark Dec 16 '11 at 10:42
  • Could you remove the printf statements in c and check if it works. printf("Calling dispatchEvent() [%p] ...\n", dispatchMessage); should perhaps be after the null check. – Sid Malani Dec 16 '11 at 10:44
  • I played around with those as well - removing the first printf or terminating it with \n causes the program to segfault. I'm guessing because the JVM doesn't have time to load and the printf is "blocking" it just enough for it to work ok. Moving the line 'printf("Calling dispatchEvent")' changes nothing; and it is used to print the pointer value of the dispatchEvent() methodID. I printed it to see if it's null or not, it's a debug message so moving it in the is-null if check defeats it's purpose. – Shark Dec 16 '11 at 10:53
  • Stack: [0xbfce0000,0xbfd30000], sp=0xbfd2f380, free space=316k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) C 0x632e6176 [error occurred during error reporting (printing native stack), id 0xb] this part tells that its failing while trying to print. So worth commenting out all printf statement. Have you tried that as well? – Sid Malani Dec 16 '11 at 11:02
  • Yes, nothing changes except if I comment out the first printf statement - the one saying printf("Creating JVM"). If I move it out, the program segfaults at the create_jvm line, the others don't seem to make any difference. Thanks for the continuous help Sid :) – Shark Dec 16 '11 at 11:17
  • I have updated the opening post with the solution. Turns out strcpy messed it up somehow or I dunno... In any case, with the latest fix in place, removing ALL printfs do not change the behaviour of the program - in other words, i have removed all output and printfs and it works flawlessly. – Shark Dec 16 '11 at 13:02

2 Answers2

2

Issue has been resolved.

After searching some more, I found this article http://docs.oracle.com/javase/1.4.2/docs/guide/jni/jni-12.html#JNI_OnLoad

Near the bottom of it is a nice example of both how to pass multiple options AND how to name them. Turns out -Xcheck:jni is passed as -DXcheck:jni (jni:pedantic works too) and after I moved the strcpy out of my code, I no longer get the crash dump at the end. I suspect the culprit was strcpy but one of these did the trick.

Here's the updated code bit:

JNIEnv* create_vm(JavaVM ** jvm)
{

    JNIEnv *env;
    JavaVMInitArgs vm_args;
    JavaVMOption options[2];
    options[0].optionString = CLASSPATH; //"-Djava.class.path=/scratch/workareas/JTest/Java/"
    options[1].optionString = "-DXcheck:jni:pedantic";
    vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
    vm_args.nOptions = 2;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = JNI_TRUE; //drop unrecognized options

    int ret = JNI_CreateJavaVM(jvm, (void**) &env, &vm_args);
    if (ret < 0) printf("\n<<<<< Unable to Launch JVM >>>>>\n");
    return env;
}
jjnguy
  • 136,852
  • 53
  • 295
  • 323
Shark
  • 6,513
  • 3
  • 28
  • 50
  • In the future please place your solution in the answer and not in the question itself. I have made the necessary edits for you this time. – jjnguy Feb 08 '12 at 15:44
0

For what it is worth, in situations like this one, running the JVM with -Xcheck:jni can sometimes spot what the eye is missing.

Edit: and I don't see anything obvious here either.

I suspect the culprit was strcpy but one of these did the trick.

The reason your strcpy made it crash is that the JavaVMOption just contains a pointer to a string, when you assign your string you make it point to the constant option string, when you strcpy something to it your are basically writing things to a pointer you have not allocated. I didn't even think about looking at such errors when you first posted your question. Sorry.

Fredrik
  • 5,759
  • 2
  • 26
  • 32
  • So how do I pass the Xcheck:jni:pedantic option to the VM? Editing the optionString and nOptions didn't work, guess I'll dig out how to do it in the meantime if you don't beat me to it :) – Shark Dec 16 '11 at 12:28
  • Like any other option, it is not a -D thing though... It should be: "options[1].optionString = "-Xcheck:jni"; – Fredrik Dec 16 '11 at 13:12
  • turns out either way is fine since I found the root cause. Thanks for the heads-up though. – Shark Dec 16 '11 at 14:06
  • 1
    @Shark ">turns out either way is fine"... Well, one instructs the JVM to check for faulty references and other JNI errors, the other one just add a system property. Big difference. – Fredrik Dec 16 '11 at 14:18
  • I understand the 'Big difference' but whether it's on or off really brings nothing new to the table or affects the situation in any way. – Shark Dec 16 '11 at 17:09
  • No, because in this case the error was the strcpy(). Had it been jni related it would have been the difference between the JVM telling you about it or not. – Fredrik Dec 16 '11 at 19:07