Mihai
Mihai

Reputation: 600

JNI native dll not returning jobjectArray crashes VM

I am developing a Java application that calls a native dll library. My code for the c++ method that I call:

JNIEXPORT jobjectArray JNICALL Java_Surf_TopSurfWrapp_computeSurfExtractDescriptor__LSurf_TopSurfVisualword_2Ljava_lang_String_2(JNIEnv *env, jclass c, jobject vw, jstring imagePath)
{
c = env->GetObjectClass(vw);
printf("start\n");
jobjectArray ret = (jobjectArray) env->NewObjectArray(100, c, vw);

for (int i = 0; i < 100; i++)
{
    jmethodID visualWordConstructor = env->GetMethodID(c, "<init>", "(IFFIFFFF)V");
    jobject element = env->NewObject(c, visualWordConstructor, 1, 1.0, 1.0, 1, 1.0, 1.0, 1.0, 1.0);
    env->SetObjectArrayElement(ret, i, element);
}
    printf("end\n");
return ret;
}

I've omitted some calculations but the problem seems to be when JNI is returning the jobjectArray. My Java code:

public class Wrapp {
    native public static TopSurfVisualword[] computeSurfExtractDescriptor(TopSurfVisualword initVW, String imagePath);
    static {
        System.loadLibrary("TopSurfWrapp");
    }

    public static void main(String[] args) {
    TopSurfVisualword[] d = TopSurfWrapp.computeSurfExtractDescriptor(new TopSurfVisualword(), "d:\\oil.jpg");
    }
}

I am getting the following JVM error:

debug:
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000076d73332, pid=3384, tid=1572
#
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) 64-Bit Server VM (21.0-b17 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [ntdll.dll+0x53332]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\Programming\Visual Studio projects\GPC\TopSurfWrapp\x64\Release\hs_err_pid3384.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
start
end
Java Result: 1

Because I'm getting the "start" and "end" messages I'm assuming that the problem is at returning the array. The problematic frame seems to be at ntdll.dll (which is not my dynamic library). Please help.

Upvotes: 0

Views: 3033

Answers (1)

Java42
Java42

Reputation: 7706

I tested your sample on my system and it runs fine.

A few hints to debug...

1) When debuging using printf in native code, follow each printf with fflush.

printf("debug message\n");
fflush(stdout);

2) Check for null return values from the JNI functions.

jmethodID visualWordConstructor = env->GetMethodID(c, "<init>", "(IFFIFFFF)V"); 
if(visualWordConstructor==0) {
    // print/fflush an error message or throw an exception or both
    // stop processing and return to java now
}

3) Printing to stdout and exceptions/errors during JNI can be tricky. Try the following to observe the behaviour.

jclass rx = env->FindClass("java/lang/RuntimeException");
printf("start\n");
fflush(stdout);

env->ThrowNew(rx, "thrown from native code");

printf("end\n");
fflush(stdout);

You will first see "end" and then see the exception.
This is expected since exceptions during JNI are queued untill the java side gets control.

Upvotes: 3

Related Questions