RFR: 8313874 - JNI NewWeakGlobalRef throws exception for null arg

Stefan Karlsson stefank at openjdk.org
Tue Aug 8 12:30:30 UTC 2023


On Tue, 8 Aug 2023 10:40:30 GMT, Oli Gillespie <ogillespie at openjdk.org> wrote:

> According to the JNI spec for NewWeakGlobalRef:
> 
>> Returns NULL if obj refers to null, or if obj was a weak global reference, or if the VM runs out of memory. If the VM runs out of memory, an OutOfMemoryError will be thrown.
> 
> The current behaviour is that `NewWeakGlobalRef(nullptr)` throws an OutOfMemoryError, which it should not.
> 
> (Thanks @dholmes-ora for confirming the issue and suggesting a fix.)

I don't see why `env->NewWeakGlobalRef(env, env->NewWeakGlobalRef(env, obj));` should return NULL, but it sounds to me like that's what the spec asks for. My guess is that the spec is ill-worded.

You can pass `jweak`s as `jobject`s. `JNIHandles::resolve` will apply the correct GC barriers depending on the underlying handle. @kimbarrett implemented that support by adding tagged bits to the handles.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/15188#issuecomment-1669517632


More information about the hotspot-dev mailing list