DetachCurrentThread corrupts; DestroyJavaVM waits!
Hello Guys!
In my static library I create 2 threads. ThreadX and ThreadY.
BothTheadX andThreadY make JNI calls to a DLL.
ThreadX and ThreadY are as follows:
ThreadX ThreadY
- -
AttachCurrentThread AttachCurrentThread
......
JNI CallsJNI Calls
......
DetachCurrentThread DetachCurrentThread
- -
However,ThreadYexits first (i.e. callsThreadY's DetachCurrentThread), after thisThreadX's further calls to JNI fails (BAD ACCESS Error)
Any solution for this?
Nowif I remove DetachCurrentThread call from these 2 threads, then my DestroyJavaVM waits for infinite time.
And if I mistake not, even after Detaching both the Threads, DestroyJavaVM still waits for release of some more resources/handles. (not very sure about this one)
Any pointers?
Please guide!
Thanks & Regards,
Pacific
> However, ThreadY exits first (i.e. calls ThreadY's DetachCurrentThread),
> after this ThreadX's further calls to JNI fails (BAD ACCESS Error)
Theoretically it should not be a problem.
I would suggest to run your application with '-Xcheck:jni' and see if there are any complains.
If it does not help post example of your source code to reproduce it.
> Now if I remove DetachCurrentThread call from these 2 threads,
> then my DestroyJavaVM waits for infinite time.
JNI Specification (http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/invocation.html#destroy_java_vm) says
"DestroyJavaVM unloads a Java VM and reclaims its resources.
Only the main thread can unload the VM. The system waits until the main thread is only remaining user thread before it destroys the VM.
"
So, it is correct behavior.
If you don't want DestroyJavaVM to wait for your threads - use AttachCurrentThreadAsDaemon instead of AttachCurrentThread.
-katya
> ...further calls to JNI fails (BAD
> ACCESS Error)
>
> Any solution for this?
>
Fix your code - something is wrong with it. It has nothing to do with the VM itself.
> And if I mistake not, even after Detaching both the
> Threads, DestroyJavaVM still waits for release of
> some more resources/handles. (not very sure about
> this one)
DestroyJavaVM does nothing in the Sun VM.