Use JNI Through JAVA Thread

P

pIm3ntO0o.

I made an example which works fine in the JAVA main
Thread, but crash in a new Thread, Why?

Is it possible to do something like that?
if no, how?

public class UserThread extends Threads{

private MyJNIObject myJNIObject;

public UserThread(){
try{
System.loadLibrary("myDLL.dll");
}catch(UnsatisfiedLinkError e){}
this.myJNIObject = new MyJNIObject();

}

public void run(){
this.myJNIObject.run();
}

}
 
F

Frank van Schie

pIm3ntO0o. said:
I made an example which works fine in the JAVA main
Thread, but crash in a new Thread, Why?

Define 'crash'? If it just hangs, see the answer below. If it actually
gives you exceptions, copy/paste them.
public void run(){
this.myJNIObject.run();
}

If you want to start a Thread object, don't use .run(), use .start().

run() is simply a method call made from the calling thread. start()
actually creates a new thread and calls the run() method of the object
representing the thread.
 
P

pIm3ntO0o.

I know that, we must launch a java thread with start() method and not
with run() method.
The run() method in my example is a method of the jni object call by
the run() method of the thread.
_________________________________________________________
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ILLEGAL_INSTRUCTION (0xc000001d) at pc=0x3b225fd5,
pid=5792, tid=4508
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_04-b05 mixed mode)
# Problematic frame:
# [thread 5304 also had an error]
C [myDLL.dll+0x375fd5]
#
# An error report file with more information is saved as
hs_err_pid5792.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
_________________________________________________________
hs_err_pid5792.log
_________________________________________________________

#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ILLEGAL_INSTRUCTION (0xc000001d) at pc=0x3b225fd5,
pid=5792, tid=4508
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_04-b05 mixed mode)
# Problematic frame:
# C [myDLL.dll+0x375fd5]
#

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

Current thread (0x00a47878): JavaThread "Finalizer" daemon
[_thread_in_native, id=4508]

siginfo: ExceptionCode=0xc000001d

Registers:
EAX=0x2ccaf830, EBX=0x22d4e7c0, ECX=0x00974ea8, EDX=0x00974ea8
ESP=0x009724c8, EBP=0x2ccaf890, ESI=0x2ccaf838, EDI=0x2ccaf890
EIP=0x3b225fd5, EFLAGS=0x00010202

Top of Stack: (sp=0x009724c8)
0x009724c8: 3b225fd4 3b225fc0 cdcdcd00 00000000
0x009724d8: cdcdcdcd 00978a80 00000000 00000000
0x009724e8: 00000000 00000000 00000000 00000000
0x009724f8: 3b207148 00000011 009776a8 00000020
0x00972508: 3b20714c 00000011 00977440 00000020
0x00972518: 3b207150 00000011 00979b10 00000020
0x00972528: 3b20714c 00000000 00000000 00000000
0x00972538: 3b20714c 00000001 00972a38 00000001

Instructions: (pc=0x3b225fd5)
0x3b225fc5: 28 eb 3a f2 1d eb 3a 5c 63 eb 3a 00 00 00 00 94
0x3b225fd5: 8f eb 3a f7 8b eb 3a 07 9f eb 3a a0 3d eb 3a d7


Stack: [0x2acb0000,0x2ccb0000), sp=0x009724c8, free space=-691447k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code,
C=native code)
C [myDLL.dll+0x375fd5]
j tools.PeerObject.destroy(J)V+0
j tools.PeerObject.destroy()V+14
j tools.PeerObject.finalize()V+1
v ~StubRoutines::call_stub
V [jvm.dll+0x82696]
V [jvm.dll+0xd6fd9]
V [jvm.dll+0x82567]
V [jvm.dll+0x87985]
V [jvm.dll+0x8fa92]
C [java.dll+0x2006]
j java.lang.ref.Finalizer.runFinalizer()V+45
j java.lang.ref.Finalizer.access$100(Ljava/lang/ref/Finalizer;)V+1
j java.lang.ref.Finalizer$FinalizerThread.run()V+11
v ~StubRoutines::call_stub
V [jvm.dll+0x82696]
V [jvm.dll+0xd6fd9]
V [jvm.dll+0x82567]
V [jvm.dll+0x822c4]
V [jvm.dll+0x9d216]
V [jvm.dll+0x101489]
V [jvm.dll+0x101457]
C [MSVCRT.dll+0x2a3b0]
C [kernel32.dll+0xb50b]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j tools.PeerObject.destroy(J)V+0
j tools.PeerObject.destroy()V+14
j tools.PeerObject.finalize()V+1
v ~StubRoutines::call_stub
j java.lang.ref.Finalizer.invokeFinalizeMethod(Ljava/lang/Object;)V+0
j java.lang.ref.Finalizer.runFinalizer()V+45
j java.lang.ref.Finalizer.access$100(Ljava/lang/ref/Finalizer;)V+1
j java.lang.ref.Finalizer$FinalizerThread.run()V+11
v ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
0x00366b88 JavaThread "DestroyJavaVM" [_thread_blocked, id=5536]
0x3ad80b80 JavaThread "Thread-0" [_thread_in_native, id=5304]
0x00a5cb60 JavaThread "Low Memory Detector" daemon [_thread_blocked,
id=2620]
0x00a5b118 JavaThread "CompilerThread0" daemon [_thread_blocked,
id=5412]
0x00a5a3b0 JavaThread "Signal Dispatcher" daemon [_thread_blocked,
id=5132]
0x00a58270 JavaThread "JDWP Command Reader" daemon
[_thread_in_native, id=4176]
0x00a57430 JavaThread "JDWP Event Helper Thread" daemon
[_thread_blocked, id=5432]
0x00a55ca8 JavaThread "JDWP Transport Listener: dt_socket" daemon
[_thread_blocked, id=5840]
=>0x00a47878 JavaThread "Finalizer" daemon [_thread_in_native, id=4508]
0x00a463e8 JavaThread "Reference Handler" daemon [_thread_blocked,
id=5256]

Other Threads:
0x00a43a18 VMThread [id=4228]
0x00a5dd98 WatcherThread [id=5316]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
def new generation total 36288K, used 1890K [0x02a90000, 0x051f0000,
0x051f0000)
eden space 32256K, 3% used [0x02a90000, 0x02b9efc8, 0x04a10000)
from space 4032K, 20% used [0x04e00000, 0x04ec9af0, 0x051f0000)
to space 4032K, 0% used [0x04a10000, 0x04a10000, 0x04e00000)
tenured generation total 483968K, used 0K [0x051f0000, 0x22a90000,
0x22a90000)
the space 483968K, 0% used [0x051f0000, 0x051f0000, 0x051f0200,
0x22a90000)
compacting perm gen total 8192K, used 2898K [0x22a90000, 0x23290000,
0x26a90000)
the space 8192K, 35% used [0x22a90000, 0x22d64b28, 0x22d64c00,
0x23290000)
No shared spaces configured.

Dynamic libraries:
0x00400000 - 0x0040c000 C:\Program
Files\Java\jdk1.5.0_04\bin\javaw.exe
0x7c910000 - 0x7c9c7000 C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c904000 C:\WINDOWS\system32\kernel32.dll
0x77da0000 - 0x77e4c000 C:\WINDOWS\system32\ADVAPI32.dll
0x77e50000 - 0x77ee1000 C:\WINDOWS\system32\RPCRT4.dll
0x77d10000 - 0x77da0000 C:\WINDOWS\system32\USER32.dll
0x77ef0000 - 0x77f37000 C:\WINDOWS\system32\GDI32.dll
0x77be0000 - 0x77c38000 C:\WINDOWS\system32\MSVCRT.dll
0x6d6b0000 - 0x6d839000 C:\Program
Files\Java\jdk1.5.0_04\jre\bin\client\jvm.dll
0x76ae0000 - 0x76b0f000 C:\WINDOWS\system32\WINMM.dll
0x6d2f0000 - 0x6d2f8000 C:\Program
Files\Java\jdk1.5.0_04\jre\bin\hpi.dll
0x76ba0000 - 0x76bab000 C:\WINDOWS\system32\PSAPI.DLL
0x6d3f0000 - 0x6d425000 C:\Program
Files\Java\jdk1.5.0_04\jre\bin\jdwp.dll
0x6d680000 - 0x6d68c000 C:\Program
Files\Java\jdk1.5.0_04\jre\bin\verify.dll
0x6d370000 - 0x6d38d000 C:\Program
Files\Java\jdk1.5.0_04\jre\bin\java.dll
0x6d6a0000 - 0x6d6af000 C:\Program
Files\Java\jdk1.5.0_04\jre\bin\zip.dll
0x6d290000 - 0x6d297000 C:\Program
Files\Java\jdk1.5.0_04\jre\bin\dt_socket.dll
0x719f0000 - 0x71a07000 C:\WINDOWS\system32\WS2_32.dll
0x719e0000 - 0x719e8000 C:\WINDOWS\system32\WS2HELP.dll
0x71990000 - 0x719d0000 C:\WINDOWS\System32\mswsock.dll
0x76ed0000 - 0x76ef7000 C:\WINDOWS\system32\DNSAPI.dll
0x76f60000 - 0x76f68000 C:\WINDOWS\System32\winrnr.dll
0x76f10000 - 0x76f3d000 C:\WINDOWS\system32\WLDAP32.dll
0x74cd0000 - 0x74cee000 C:\WINDOWS\system32\wshbth.dll
0x778e0000 - 0x779d8000 C:\WINDOWS\system32\SETUPAPI.dll
0x76f70000 - 0x76f76000 C:\WINDOWS\system32\rasadhlp.dll
0x62e40000 - 0x62e99000 C:\WINDOWS\system32\hnetcfg.dll
0x719d0000 - 0x719d8000 C:\WINDOWS\System32\wshtcpip.dll
0x00920000 - 0x00942000
D:\project\visual\COTS\COTSLib\Debug\COTSLib.dll
0x3aeb0000 - 0x3b293000 D:\project\parser\dbgbin\myDLL.dll
0x71a10000 - 0x71a1a000 C:\WINDOWS\system32\WSOCK32.dll
0x3b2a0000 - 0x3b30c000 C:\WINDOWS\system32\MSVCRTD.dll
0x00950000 - 0x0096e000
D:\project\visual\COTS\ByteBuffer\Debug\ByteBuffer.dll
0x3b310000 - 0x3b38e000 C:\WINDOWS\system32\MSVCP60D.dll

VM Arguments:
jvm_args: -Xms512m -Xmx512m -Xss32m -Xcheck:jni -verbose:jni -Xdebug
-Xnoagent
-Xrunjdwp:transport=dt_socket,suspend=y,address=localhost:2119
java_command: OutlineApplication

Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_04
PATH=D:\project\visual\COTS\COTSLib\Debug;D:\project\parser\dbgbin;D:\project\visual\COTS\ByteBuffer\Debug;
USERNAME=whoami
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 13 Stepping 6, GenuineIntel



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

OS: Windows XP Build 2600 Service Pack 2

CPU:total 1 family 6, cmov, cx8, fxsr, mmx, sse, sse2

Memory: 4k page, physical 1047344k(534160k free), swap
2518516k(1132120k free)

vm_info: Java HotSpot(TM) Client VM (1.5.0_04-b05) for windows-x86,
built on Jun 3 2005 02:10:41 by "java_re" with MS VC++ 6.0


_________________________________________________________
 
G

Gordon Beaton

I know that, we must launch a java thread with start() method and
not with run() method. The run() method in my example is a method of
the jni object call by the run() method of the thread.

To answer your original question, yes it is possible to use native
code in a Threaded Java application. Consider that many of the
standard libraries use native methods.

From your thread dump I strongly suspect an error in your
native code related to pointer misuse.

/gordon
 
P

pIm3ntO0o.

You are right.
I test a simple jni call in a java thread and that works fine!

Any idea for reason of crash?
 
D

Dale King

pIm3ntO0o. said:
C [myDLL.dll+0x375fd5]
j tools.PeerObject.destroy(J)V+0
j tools.PeerObject.destroy()V+14
j tools.PeerObject.finalize()V+1
v ~StubRoutines::call_stub
V [jvm.dll+0x82696]
V [jvm.dll+0xd6fd9]
V [jvm.dll+0x82567]
V [jvm.dll+0x87985]
V [jvm.dll+0x8fa92]
C [java.dll+0x2006]
j java.lang.ref.Finalizer.runFinalizer()V+45
j java.lang.ref.Finalizer.access$100(Ljava/lang/ref/Finalizer;)V+1
j java.lang.ref.Finalizer$FinalizerThread.run()V+11
v ~StubRoutines::call_stub

This stack trace says the crash occurred when the JVM was shutting down
and it was running a finalizer that was calling a native method. I'm
guessing that you already freed an object and are trying to access it again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top