JNI and C++ exceptions

Vadim Pakhnushev vadim.pakhnushev at oracle.com
Fri Sep 27 02:44:53 PDT 2013


BTW, operator new doesn't return NULL if allocation is unsuccessful, it 
throws bad_alloc instead, so "if (!obj)" after new is non-standard and 
will not work on any recent compiler.

Thanks,
Vadim

On 27.09.2013 13:10, Artem Ananiev wrote:
> Hi,
>
> here is a short question. In some JDK implementations (e.g. IBM's), 
> JNI may throw C++ exceptions. For example, env->NewByteArray() throws 
> bad_alloc. Right now in JDK/JavaFX native code we don't protect 
> against that. Should we? An anti-pattern that some source scanning 
> tools warn about is
>
> <type> obj = new <type>();
> if (!obj) {
>   // handle that
> }
> env->SomeJNICall();
>
> If SomeJNICall() throws an exception, "obj" will never be deleted. 
> Protection against bad_alloc doesn't make much sense, since we're out 
> of memory anyway and will unlikely be able to recover, but what about 
> other exceptions?
>
> Thanks,
>
> Artem



More information about the openjfx-dev mailing list