Gregory, Since ExceptionClear() zeroes reference to exception object, it may be collected before subsequent call to NewGlobalRef() can pin it. The only safe way to prevent exception from garbage collection and be able to report it to debugger is to call NewGlobalRef() before ExceptionClear(). I cannot find any other solution.
Though this does not follow well JNI spec (which is quite vague in many points), it works with RI. And I'd like NewGlobalRef() to be fixed and don't return NULL in this case. Do you agree? Thanks. Ivan On 3/23/07, Gregory Shimansky <[EMAIL PROTECTED]> wrote:
Ivan Popov wrote: > Working on HARMONY-3304 [1] I noticed problem with JNI function > NewGlobalRef() in DRLVM. If it is called for a pending exception > object before ExceptionClear() is invoked, it returns NULL, which is > interpreted as out of memory according to JNI spec [2]. This causes > errors in debug support. In Sun/BEA VM non-NULL reference is returned > in this case. Actually JNI specification [1] states that only 3 JNI functions may be safely called in exception state. Looking at the code of NewGloabRef I see that the check for exception was added at revision 489694 as a result of committing HARMONY-2817. > Simple workaround is to call ExceptionClear() before NewGlobalRef(). > However, I'm not sure if ExceptionClear() won't dispose exception ExceptionClear zeroes a reference to exception object in TLS which is considered as no exception state. The object itself may be collected later if there are no live references to it left. > object itself. To my mind it's better to fix NewGlobalRef() and make > it compatible with RI. [1] http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp17626 -- Gregory
