简体   繁体   English

致命信号 11 (SIGSEGV),在第二次 JNI String 调用 Java 方法之后

[英]Fatal signal 11 (SIGSEGV), after second JNI String call to Java method

I am kinda stuck and lost on this problem: I want to pass a string parameter from a C++ Method (Native C), that I call via ROS to a Java method.我有点被这个问题迷住了:我想将一个字符串参数从我通过 ROS 调用的 C++ 方法(本机 C)传递给 Java 方法。 In this java method, a robot I am programming should use some Text-to-Speak internal method to say the written thing (the tts method is given by the company of the robot).在这个java方法中,我正在编程的机器人应该使用一些Text-to-Speak内部方法来说写的东西(tts方法是机器人公司提供的)。 But whenever I call this Method in C++ from ROS, the first call is properly handled, but on the second call the program crashes with the previous stated error code.但是每当我从 ROS 在 C++ 中调用此方法时,第一次调用都得到了正确处理,但在第二次调用时程序崩溃并出现了前面所述的错误代码。 Here is my C++ code: (myEnv/myVM/myClass are set before in the init method) (log is just a small own method which does System.out.println() for C++)这是我的 C++ 代码:(myEnv/myVM/myClass 之前在 init 方法中设置)(log 只是一个小的自己的方法,它为 C++ 执行 System.out.println())

void onSpeech(const std_msgs::String::ConstPtr &msg){
    log("RECEIVED SPEECH");
    log(msg->data.c_str());

    myVM->AttachCurrentThread(&myEnv, nullptr);
    jstring stringMsg = myEnv->NewStringUTF(msg->data.c_str());
    log("CALL JAVA METHOD");
    jmethodID mid = myEnv->GetStaticMethodID(myClass, "setSpeech", "(Ljava/lang/String;)V");
    myEnv->CallStaticVoidMethod(myClass, mid, stringMsg);

    log("START WAITING");
    ros::Time begin = ros::Time().now();
    while(begin + ros::Duration(2,0) > ros::Time().now()){ }
    log("WAIT FINISHED");

    log("CALL RELEASE");
    myEnv->ReleaseStringUTFChars(stringMsg, msg->data.c_str());
    myVM->DetachCurrentThread();
}

And here is the Java Method we call.这是我们调用的 Java 方法。 I copy the passed String so often, because I want to copy the data of the word and not the pointer, and the C++ Code can release the stringMsg variable without affecting the java code.我经常复制传递的String,因为我想复制的是word的数据而不是指针,C++代码可以释放stringMsg变量而不影响java代码。

public static void setSpeech(String text) {
    System.out.println("JAVA CALL");
    System.out.println("PASSED TEXT: " + text);
    char[] newChar = text.toCharArray();
    System.out.println("CHAR: " + Arrays.toString(newChar));
    String newString = String.copyValueOf(newChar);
    System.out.println("NEW STRING: " + newString);
    //Below is just the ttsmethod like this: startSpeak(newString)
}

This is the complete Log, I first send a msg with the data "Hello 1" which worked as expected and the robot said it correct.这是完整的日志,我首先发送了一条消息,其中包含数据“Hello 1”,它按预期工作,机器人说它是正确的。 But after that I send another message with the data "Hello 2" and the program crashes before the newChar could be set.但在那之后我发送了另一条消息,其中包含数据“Hello 2”,程序在设置 newChar 之前崩溃了。 Which in my mind doesn't make sense, because we even System.out.println() the text one line before.这在我看来是没有意义的,因为我们甚至在 System.out.println() 前一行的文本。

I/CPP: Hello 1
I/CPP: CALL JAVA METHOD
I/System.out: JAVA CALL
I/System.out: PASSED TEXT: Hello 1
I/System.out: CHAR: [H, e, l, l, o,  , 1]
I/System.out: NEW STRING: Hello 1
I/CPP: START WAITING
I/System.out: SPEECH STARTED: Hello 1
I/System.out: SPEECH FINISHED
I/CPP: WAIT FINISHED
I/CPP: CALL RELEASE
//Now I call the method again
I/CPP: RECEIVED SPEECH
I/CPP: Hello 2
I/CPP: CALL JAVA METHOD
I/System.out: JAVA CALL
I/System.out: PASSED TEXT: Hello 2
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x38206f in tid 8890 (Thread-117)

Any help would be appreciated, I'm really lost why the code works in the "PASSED TEXT" line and then in the newChar line it suddenly crashes.任何帮助将不胜感激,我真的不知道为什么代码在“PASSED TEXT”行中工作,然后在 newChar 行中突然崩溃。 Thanks谢谢

btw this is the logcat output for "Hello 9" and "Hello 9"顺便说一句,这是“Hello 9”和“Hello 9”的 logcat 输出

2022-12-19 13:44:28.785 9455-9499/rob.rosloomo I/ROSCPP_NDK: RECEIVED SPEECH
2022-12-19 13:44:28.786 9455-9499/rob.rosloomo I/ROSCPP_NDK: Hello 9
2022-12-19 13:44:28.787 9455-9499/rob.rosloomo I/ROSCPP_NDK: CALL JAVA METHOD
2022-12-19 13:44:28.788 9455-9499/rob.rosloomo I/System.out: JAVA CALL
2022-12-19 13:44:28.788 9455-9499/rob.rosloomo I/System.out: PASSED TEXT: Hello 9
2022-12-19 13:44:28.788 9455-9499/rob.rosloomo A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x39206f in tid 9499 (Thread-130)
2022-12-19 13:44:28.893 2750-2750/? I/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2022-12-19 13:44:28.893 2750-2750/? I/DEBUG: Build fingerprint: 'i-Buddie/VA50EC_i_Buddie/VA50EC_1:5.1.1/LMY47Z/78:user/test-keys'
2022-12-19 13:44:28.893 2750-2750/? I/DEBUG: Revision: '0'
2022-12-19 13:44:28.893 2750-2750/? I/DEBUG: ABI: 'x86'
2022-12-19 13:44:28.894 2750-2750/? I/DEBUG: pid: 9455, tid: 9499, name: Thread-130  >>> rob.rosloomo <<<
2022-12-19 13:44:28.894 2750-2750/? I/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x39206f
2022-12-19 13:44:28.920 2750-2750/? I/DEBUG:     eax f3cd0040  ebx f3bfca2c  ecx f3cd0000  edx 00000001
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     esi 0039206f  edi f3ef006c
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     xcs 00000023  xds 0000002b  xes 0000002b  xfs 00000097  xss 0000002b
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     eip f38b4e12  ebp e2a23d78  esp e2a23d40  flags 00210206
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG: backtrace:
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     #00 pc 001a5e12  /system/lib/libart.so (art::gc::allocator::RosAlloc::RefillRun(art::Thread*, unsigned int)+290)
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     #01 pc 001a60fa  /system/lib/libart.so (art::gc::allocator::RosAlloc::AllocFromRun(art::Thread*, unsigned int, unsigned int*, unsigned int*, unsigned int*)+666)
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     #02 pc 00155f8d  /system/lib/libart.so (art::mirror::Object* art::gc::space::RosAllocSpace::AllocCommon<true>(art::Thread*, unsigned int, unsigned int*, unsigned int*, unsigned int*)+109)
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     #03 pc 0040571d  /system/lib/libart.so (_ZN3art6mirror5Array5AllocILb0EEEPS1_PNS_6ThreadEPNS0_5ClassEijNS_2gc13AllocatorTypeEb.constprop.104+989)
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     #04 pc 00406a72  /system/lib/libart.so (artAllocArrayFromCodeResolvedRosAlloc+162)
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     #05 pc 000e5815  /system/lib/libart.so (art_quick_alloc_array_resolved_rosalloc+37)
2022-12-19 13:44:28.921 2750-2750/? I/DEBUG:     #06 pc 00079bfb  /data/dalvik-cache/x86/system@framework@boot.oat
2022-12-19 13:44:29.120 2750-2750/? I/DEBUG: Tombstone written to: /data/tombstones/tombstone_06
2022-12-19 13:44:29.120 3124-9562/? W/ActivityManager:   Force finishing activity 1 rob.rosloomo/.MainActivity
2022-12-19 13:44:29.129 3124-3255/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
2022-12-19 13:44:29.131 3124-9562/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
2022-12-19 13:44:29.131 3124-9562/? W/ActivityManager: Exception thrown during pause

https://stackoverflow.com/users/882003/john had the right tipp with this comment: ReleaseStringUTFChars should only be called on strings allocated with GetStringUTFChars . https://stackoverflow.com/users/882003/john的评论是正确的: ReleaseStringUTFChars should only be called on strings allocated with GetStringUTFChars Been a long time since I've done JNI but remove that line and I think you are OK自从我完成 JNI 以来已经有很长时间了,但是删除了那行,我认为你还可以

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

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