分配字符串时EXCEPTION_ACCESS_VIOLATION

问题描述 投票:-2回答:1

我试图通过jni将从java应用程序传递的字符串分配给c ++类实例的字段,但是仍然会收到似乎来自无处的错误。我的应用程序在本机代码中崩溃,除了配合之外什么都不做。

危机发生的JNI方法

[SCVXManager.java]

public static native void configure(String server, String realm, String username, String accountHandle);

[SCVXManager.cpp]

JNIEXPORT void JNICALL Java_mods_voicechat_SCVXManager_configure(JNIEnv * jenv, jclass jcl, jstring server,
                                                                 jstring realm, jstring username, jstring accountHandle) {

    auto app = SCVXApp::getApp();

    auto serverStr = jenv->GetStringUTFChars(server, NULL);
    auto realmStr = jenv->GetStringUTFChars(realm, NULL);
    auto usernameStr = jenv->GetStringUTFChars(username, NULL);
    auto accountHandleStr = jenv->GetStringUTFChars(accountHandle, NULL);

    app->configure(serverStr, realmStr, usernameStr, accountHandleStr);

    jenv->ReleaseStringUTFChars(server, serverStr);
    jenv->ReleaseStringUTFChars(realm, realmStr);
    jenv->ReleaseStringUTFChars(username, usernameStr);
    jenv->ReleaseStringUTFChars(accountHandle, accountHandleStr);
}

发生崩溃的相关变量的本机方法的定义。

[SCVXApp.cpp]

void SCVXApp::configure(const string &server, const string &realm, const string &username, const string &accountHandle) {
    m_server = server;
    m_realm = realm;
    m_username = username;
    m_accountHandle = accountHandle;
}

[SCVXApp.h]

class SCVXApp {

private:
    string m_realm;
    string m_server;
    string m_accountHandle;
    string m_username;

    <...>
}

崩溃堆栈跟踪

Current thread (0x00000000050d5800):  JavaThread "Main thread" [_thread_in_native, id=22916, stack(0x0000000004ed0000,0x0000000004fd0000)]

siginfo: ExceptionCode=0xc0000005, writing address 0x00007ffbf2a6cb64

Registers:
RAX=0x00000000cccccccc, RBX=0x000000003e7e69d8, RCX=0x00007ffbf2a6cb64, RDX=0x000000002614e2e0
RSP=0x0000000004fcce58, RBP=0x0000000004fcd0f0, RSI=0x00000000c1720228, RDI=0x0000000004fcce80
R8 =0x0000000000000020, R9 =0x0000000004fccfd0, R10=0x000000002614e2e0, R11=0x00007ffbf2a6cb64
R12=0x0000000000000000, R13=0x000000003e7e69d8, R14=0x0000000004fcd128, R15=0x00000000050d5800
RIP=0x00007ffbf2a316e9, EFLAGS=0x0000000000010246

Top of Stack: (sp=0x0000000004fcce58)
0x0000000004fcce58:   00007ffbf2a6594c cccccccccccccccc
0x0000000004fcce68:   cccccccccccccccc cccccccccccccccc
0x0000000004fcce78:   cccccccccccccccc 0000000004fcced0
0x0000000004fcce88:   00007ffbf2a65578 00007ffbf2a6cb64
0x0000000004fcce98:   000000002614e2e0 0000000000000020
0x0000000004fccea8:   cccccccccccccccc 0000000004fccf40
0x0000000004fcceb8:   00007ffbf2a6cb64 cccccccccccccccc
0x0000000004fccec8:   cccccccccccccccc 0000000004fccf10
0x0000000004fcced8:   00007ffbf2a697da 0000000004fccf40
0x0000000004fccee8:   000000002614e2e0 0000000000000020
0x0000000004fccef8:   cccccccccccccccc 0000000004fcd020
0x0000000004fccf08:   cccccccccccccccc 0000000004fccf40
0x0000000004fccf18:   00007ffbf2a67905 0000000004fccf40
0x0000000004fccf28:   0000000004fcd020 cccccccccccccccc
0x0000000004fccf38:   cccccccccccccccc 0000000004fcd060
0x0000000004fccf48:   00007ffbf2a6cb64 0000000004fccec0 

Instructions: (pc=0x00007ffbf2a316e9)
0x00007ffbf2a316c9:   8b df 48 8b f9 49 8b c8 4c 8b c6 49 8b f2 f3 a4
0x00007ffbf2a316d9:   49 8b f0 49 8b fb c3 0f 10 02 41 0f 10 4c 10 f0
0x00007ffbf2a316e9:   0f 11 01 41 0f 11 4c 08 f0 48 8b c1 c3 66 66 0f
0x00007ffbf2a316f9:   1f 84 00 00 00 00 00 48 8b c1 4c 8d 0d f6 e8 ff 


Register to memory mapping:

RAX=
[error occurred during error reporting (printing register info), id 0xc0000005]

Stack: [0x0000000004ed0000,0x0000000004fd0000],  sp=0x0000000004fcce58,  free space=1011k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [VCRUNTIME140D.dll+0x16e9]
C  [vivox_test.dll+0x594c]  std::char_traits<char>::move+0x3c
C  [vivox_test.dll+0x5578]  std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign+0x78
C  [vivox_test.dll+0x97da]  std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=+0x8a
C  [vivox_test.dll+0x7905]  SCVXApp::configure+0x45
C  [vivox_test.dll+0xcb64]  Java_net_core_processing_SCVXManager_configure+0x134
C  0x00000000051f09cc

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  net.core.processing.SCVXManager.configure(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V+0
j  net.core.processing.SCVXController.load(Lnet/common/event/InitializationEvent;)V+39
v  ~StubRoutines::call_stub
<...>

值得注意的是,在此之前的任何地方都没有引用configure函数中分配的字段。

如果这很重要vivox_test.dll是我用MSBuild使用CMake构建的共享库

我不明白是什么导致了这个问题,我该如何解决这个问题。

java c++ java-native-interface
1个回答
-1
投票

不要使用移动语义。 (从4个参数中删除“&”,在C ++函数中只留下“string”)。使用副本。可能与jenv返回的字符串有关的移动相关问题。另外,检查(调试)从jenv返回的字符串。

© www.soinside.com 2019 - 2024. All rights reserved.