调用 NewStringUTF() 时崩溃

Crash while calling NewStringUTF()

我正在编写一个 JNI 来从 Java 调用 C++ 代码。当我使用 NewStringUTF() 时它已经崩溃了。这是我的代码:

__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of 3 is %d", 3);
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of id is %s", id.c_str());

jstring jsId = m_env->NewStringUTF(id.c_str());// CODE CRASHES HERE

__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of 4 is %d", 4);

输出为:

V/myapplication ( 4930): The value of 3 is 3
V/myapplication( 4930): The value of id is 999999999999@example.example.net
F/art     ( 4930): art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: thread Thread[12,tid=5100,Native,Thread*=0xa1033400,peer=0x12c5b0e0,"QtThread"] using JNIEnv* from thread Thread[17,tid=5359,Runnable,Thread*=0xb4e0ac00,peer=0x12e37080,"Thread-161252"]
F/art     ( 4930): art/runtime/check_jni.cc:65]     in call to NewStringUTF
F/art     ( 4930): art/runtime/check_jni.cc:65]     from void

我在Android 5 上调试时崩溃了,但是低版本没问题。 有什么建议吗?

已编辑:我更改了 m_env。这是我的代码:

JNIEnv *env = NULL;
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of 3 is %d", 3);
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of jid is %s", jid.c_str());

jstring jsJid = env->NewStringUTF(jid.c_str());
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of 4 is %d", 4);

输出为:

F/libc    (28181): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 28354 (QtThread)

JNIEnv 对象只能由请求指针的特定线程使用。在这种情况下,您的 QtThread 似乎存储了 m_env 指针,但现在它正被一个新的未命名线程 (Thread-161252) 使用。

查看 Android JNI Tips 页面了解更多信息。