2016-08-22 2 views
2

JNI를 통해 Java를 호출하는 C++를 실행하는 Windows 서비스가 있습니다.JNI의 자바 충돌 네이티브

Windows 서비스 관리자를 사용하여 서비스를 중지하면 서비스가 중단됩니다. 다음 JNI 레지스터 ++

C :

SAPPHIRE_DLL_API int sapphire_adapter_initialize(CSACallbacks* callbacks) 
{ 
JavaVMInitArgs vm_args; 
JavaVMOption options[4]; 
int flow = 0; 

g_pPumpCallbacks = callbacks; 
for (flow = 0; flow == 0; flow++) 
{ 
    //options[0].optionString = "-Djava.class.path=SapphireAdapter.jar;hdpservices.jar;..\\virgo-tomcat-server-3.6.1.RELEASE\\pickup\\com.hospira.hdpservices_1.0.0.jar;slf4j-api.jar;slf4j-log4j12.jar;log4j.jar"; 
    options[0].optionString = "-Djava.class.path=SapphireAdapter.jar;hdpservices.jar;slf4j-api.jar;slf4j-log4j12.jar;log4j.jar"; 
    options[1].optionString = "-verbose:jni"; /* print JNI-related messages */ 
    //options[2].optionString = "-Xdebug"; 
    //options[3].optionString = "-Xrunjdwp:transport=dt_socket,address=8998,server=y,suspend=y"; /* Enable Remote Debugging */ 
    vm_args.version = JNI_VERSION_1_8; 
    vm_args.nOptions = 2; // set to 2 for verbose output from jni or 4 for remote debugging. 
    vm_args.options = &options[0]; 
    vm_args.ignoreUnrecognized = 0; 
    jint res = JNI_CreateJavaVM(&g_jvm, (void **)&g_env, &vm_args); 
    if (res != 0) 
    { 
     jint result = JNI_GetCreatedJavaVMs(&g_jvm, 1, NULL); 
     if(result != JNI_OK) 
      break; 
     result = g_jvm->GetEnv((void**)&g_env, JNI_VERSION_1_8); 
    } 
    if (! g_masterClassDef.cls) 
     g_masterClassDef.cls = (jclass)g_env->NewGlobalRef(g_env->FindClass("com/hospira/sapphire/adapter/SapphireAdapter")); 
    if (! g_masterClassDef.cls) 
     break; 
    g_masterClassDef.mtd_constructor = g_env->GetMethodID(g_masterClassDef.cls, "<init>", "()V"); 
    g_masterClassDef.mtd_setReference = g_env->GetMethodID(g_masterClassDef.cls, "setReferenceId", "(J)V"); 
    g_masterClassDef.mtd_getReference = g_env->GetMethodID(g_masterClassDef.cls, "referenceId", "()J"); 
    g_masterClassDef.mtd_connect = g_env->GetMethodID(g_masterClassDef.cls, "connect", "(Ljava/lang/String;I)V"); 
    g_masterClassDef.mtd_disconnect = g_env->GetMethodID(g_masterClassDef.cls, "disconnect", "()V"); 
    g_masterClassDef.mtd_cleanup = g_env->GetStaticMethodID(g_masterClassDef.cls, "cleanup", "()V"); 
    OutputDebugString(_T("g_masterClassDef.mtd_cleanup is 0 ? ")); 
    (g_masterClassDef.mtd_cleanup == 0) ? OutputDebugString(_T(" YES")) : OutputDebugString(_T(" no")) ; 

    g_masterClassDef.mtd_sendRegistration = g_env->GetMethodID(g_masterClassDef.cls, "sendRegistrationSignal", "([B)Z"); 
    g_masterClassDef.mtd_sendClinicalStatus = g_env->GetMethodID(g_masterClassDef.cls, "sendClinicalStatusSignal", "([B)Z"); 
    g_masterClassDef.mtd_sendLogDownload = g_env->GetMethodID(g_masterClassDef.cls, "sendLogDownloadSig", "([B)Z"); 
    g_masterClassDef.mtd_sendDeployUpdateInquiry = g_env->GetMethodID(g_masterClassDef.cls, "sendDeployUpdateInquiry", "([B)Z"); 
    g_masterClassDef.mtd_sendTimeSync = g_env->GetMethodID(g_masterClassDef.cls, "sendTimeSync", "([B)Z"); 
    g_masterClassDef.mtd_setIdleTimes = g_env->GetStaticMethodID(g_masterClassDef.cls, "setIdleTimes", "(II)V"); 
    } 

    return flow; 
} 

은 내가 g_masterClassDef.mtd_cleanup가 0을 참조로 등록이 성공적으로 알고 - 아니 DebugView와에.)

CCommunicationToMedNet::~CCommunicationToMedNet(void) 
{ 
      sapphire_adapter_uninitialize(); 
      d_LoggerWrite(CMediatorLogging::e_Severity_Info, _T("Uninitialized sapphire adaptor")); 
} 

인터페이스 클래스 SapphireAdapter의 sapphire_adapter_uninitialize (Java 메소드에 도달되지 않습니다으로 자바 코드는 중요하지 않습니다)

SAPPHIRE_DLL_API void sapphire_adapter_uninitialize() 
    { 
     g_env->CallStaticVoidMethod(g_masterClassDef.cls, g_masterClassDef.mtd_cleanup); 
     g_env->DeleteGlobalRef(g_masterClassDef.cls); 
     g_jvm->DestroyJavaVM(); 
     g_jvm = NULL; 
     g_env = NULL; 
     g_pPumpCallbacks = NULL; 
    } 

3 :

는 C++ 코드는 다음 호출합니다. 내가 찾은 오류 보고서에 더 찾고

Stack: [0x0000000000210000,0x0000000000310000], sp=0x000000000030f260,  free space=1020k 

Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) 
V [jvm.dll+0x31165] 
V [jvm.dll+0x274b86] 
V [jvm.dll+0x212ad2] 
V [jvm.dll+0x32cf42] 
V [jvm.dll+0x212e63] 
V [jvm.dll+0x147a68] 
V [jvm.dll+0x14d608] 
C [SapphireAdapter.dll+0x12bc] 
C [SapphireAdapter.dll+0x169e] 
C [MedNetMediatorService.exe+0xe2746] 
C [MedNetMediatorService.exe+0xe3929] 
C [MedNetMediatorService.exe+0xe9c52] 
C [MSVCR110.dll+0x22fe1] 
C [MedNetMediatorService.exe+0x170616] 
C [kernel32.dll+0x159bd] 
C [ntdll.dll+0x2a2e1] 

4) :

Internal exceptions (10 events): 
Event: 0.149 Thread 0x00000000005bf000 Exception <a >'java/lang/ArrayIndexOutOfBoundsException': 22 (0x00000000d7240d58) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u74\6087\hotspot\src\share\vm\interpreter\interpreterRuntime.cpp, line 366] 
Event: 0.150 Thread 0x00000000005bf000 Exception <a 'java/lang/ArrayIndexOutOfBoundsException': 160> (0x00000000d72431a8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u74\6087\hotspot\src\share\vm\interpreter\interpreterRuntime.cpp, line 366] 

5) 스택 추적을 보면이 -이 코드는 JVM 방법 해결하는 방법에 대한 아이디어를 도달하지 않을 것으로 보인다 문제?

답변

2

mtd_cleanup은 정적 메서드이므로 CallStaticVoidMethod에 의해 호출되어야합니다.

+0

예 - CallStaticVoidMethod로 변경되었지만 여전히 충돌이 발생합니다. 원본 코드를 업데이트했습니다. 어떤 생각? –

+0

@ YaronBerlinsky 전체 사진을 보지 않고도 추측하기는 어렵습니다. 우리가 컴파일하고 실행할 수 있도록 최소한의 검증 가능한 예제를 만들어보십시오. – apangin