简体   繁体   English

返回jintArray时的Java JNI - EXCEPTION_ACCESS_VIOLATION

[英]Java JNI - EXCEPTION_ACCESS_VIOLATION while returning jintArray

A really weird thing happens in this code 这段代码中发生了一件非常奇怪的事情

    JNIEXPORT jintArray JNICALL Java_jsdr_SdrLibrary_getTunerGains
     (JNIEnv * env, jclass obj, jlong pointer) {
      rtlsdr_dev_t * dev;
      int * gains;
      jintArray ji;
      jint * buff;
      int i;
      int size;

      dev = (rtlsdr_dev_t *) pointer;
      size = rtlsdr_get_tuner_gains(dev, gains);

      if (size <= 0)
        .. throws an error, irrelevant code ..

      buff = (jint *) malloc(size * sizeof(jint));
      for (i = 0; i < size; i++) buff[i] = gains[i];

      ji = (*env)->NewIntArray(env, size);
      (*env)->SetIntArrayRegion(env, ji, 0, size, buff);
      return ji;
  }

The method actually returns a result that I can handle in Java with 该方法实际上返回了我可以用Java处理的结果

  System.out.println(printArray(SdrLibrary.getTunerGains(pointer)));

where printArray is just a simple printArray只是一个简单的地方

public static String printArray(int[] arr) {
    String buff = "["+arr[0];
    for (int i = 1; i < arr.length; i++) buff+=", "+arr[i];
    return buff+"]";
}

The output is the following 输出如下

[-10, 15, 40, 65, 90, 115, 140, 165, 190, 215, 240, 290, 340, 420, 430, 450, 470, 490]
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d86c2ff, pid=7160, tid=5880
#
# JRE version: 6.0_31-b05
# Java VM: Java HotSpot(TM) Client VM (20.6-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# V  [jvm.dll+0x7c2ff]
#
# An error report file with more information is saved as:
# C:\Users\Marto\workspace\JSDR\hs_err_pid7160.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

As you can see it returns a result but immediately after that the JM crashes and no further lines get executed. 正如您所看到的,它会返回一个结果,但在此之后JM崩溃并且不再执行其他行。 I would definitely appreciate any help! 我一定会感激任何帮助! Thank you! 谢谢!

EDIT: If I remove the call to rtlsdr_get_tuner_gains and instead initialize the gains array manually, no error is observed. 编辑:如果我删除对rtlsdr_get_tuner_gains的调用,而是手动初始化gain数组,则不会发现错误。 Since this method is inside a dll, can I prevent it somehow from crashing the JNI? 由于此方法在dll中,我可以以某种方式防止它崩溃JNI吗?

I discovered the source of the error. 我发现了错误的来源。 It turns out that the original library does not allocate the buffer that is being passed to it resulting in overwriting memory which is not held by the JNI (thanks to QuantumMechanic to giving me the simple idea to actually look at the source of the library). 事实证明原始库没有分配传递给它的缓冲区,导致覆盖JNI没有的内存(感谢QuantumMechanic给我一个简单的想法来实际查看库的来源)。

The solution was simple enough. 解决方案很简单。 Change this 改变这个

  int * gains;

to this 对此

  int gains[30];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM