2

I'm trying to return a jintArray from C++ to Java but no matter what I do the call keeps hanging and the code just stops. Even with something simple like this

JNIEXPORT jintArray JNICALL Java_main_getIntArray(JNIEnv *env, jclass c) {
        jintArray intArray = env->NewIntArray(5);
        jint values[5] = {69, 69, 69, 69, 69};

        env->SetIntArrayRegion(intArray, 0, 5, values);
        env->ReleaseIntArrayElements(intArray, values, NULL);
        return intArray;
    }

In java I'm doing

System.out.println("Start getting array");
System.out.println("Array: " + Arrays.toString(getIntArray()));
System.out.println("Done getting array");

but the only output I get is

Start getting array

Is there something I'm doing wrong?

  • I tried creating a pointer from values[] and using that one in the SetIntArrayRegion

  • I tried populating the array myself by looping over it

  • I tried removing the ReleaseIntArrayElements

MicroHat11
  • 227
  • 3
  • 12
  • 3
    `ReleaseIntArrayElements` shouldn't be there, since you're not calling `GetIntArrayElements`. – Michael Aug 14 '15 at 13:22
  • Just for clarity, since your code doesn't show it: your function is somehow wrapped in an `extern "C"`? (I assume it is, or you should get an `UnsatisfiedLinkError` or something of the sort) – zenzelezz Aug 14 '15 at 13:59
  • @zenzelezz: Yes, the header file is wrapped into an extern "C". Even without **ReleaseIntArrayElements** the application hangs – MicroHat11 Aug 14 '15 at 15:27
  • First, check that your native method is ever called. Start commenting out all its body, returning **null**. Then, uncomment contents one line at a time. Maybe, you can add some printf to check what happens inside the native method. – Alex Cohn Aug 16 '15 at 06:52
  • When dealing with this type of issue I often put a print statement (use cerr if C++, fprintf(stderr) if C) in the C/C++ code after every statement. Not very elegant, but it will pinpoint to the exact line where you code gives you problems, and you get an answer quickly. – Alain Aug 19 '15 at 13:10

0 Answers0