2016-09-26 41 views
3

當我嘗試編譯C++代碼以在JAVA端使用時,似乎遇到了很大的問題。當我嘗試執行任何與IO /流/操作相關的任何操作時,JVM似乎崩潰了。C++ JNI在執行任何IO /流時崩潰/掛起

有時,當我運行應用程序時,每10次運行一次,JNI與所有IO東西一起工作100%。然後剩下的時間JVM崩潰或掛起。

我使用:的Windows 10 64,JAVA jdk1.8.0_102上,NetBeans 8.1,G ++,附加選項:-D__int64 =的int64_t

當我做這些,JNI的正常工作:

JNIEXPORT jstring JNICALL Java_Main_nativePrint 
    (JNIEnv *env, jobject) 
{ 
    return env->NewStringUTF("HELLO"); 
} 

JNIEXPORT jstring JNICALL Java_Main_nativePrint 
    (JNIEnv *env, jobject) 
{ 
    double var = std::sqrt(100); 
    return env->NewStringUTF("HELLO"); 
} 

當我試圖合併的內容,如printfstd::stringstreamsprintfcout,或puts,JVM的崩潰。甚至實例化std::stringstream似乎讓我崩潰。我認爲其他種類的操作功能也崩潰了我:

Java_Main_nativePrint(JNIEnv *env, jobject instance) 
{ 
    int variable = 100; 
    char str[15]; 

    //CRASHES ON HERE 
    sprintf(str, "%d", variable); 
    //CRASHES ON HERE IF ABOVE COMMENTED OUT OR USING ANOTHER CHAR ARRAY. 
    puts(str); //CRASHES ON HERE 

    return env->NewStringUTF(str); 
} 

JNIEXPORT jstring JNICALL Java_Main_nativePrint 
    (JNIEnv *env, jobject) 
{ 
    double res = std::sqrt(100); 

    //CRASHES HERE ON INSTANTATION OF stringstream 
    std::stringstream ss; 
    //ss << std::sqrt(100); 
    //const char* str = ss.str().c_str(); 

    return env->NewStringUTF("HELLO"); 
} 

我似乎有類似的問題,以這個人:

JNI with c++ keeps crashing on cout, printf and method calls

這裏是我的轉儲:

# 
# A fatal error has been detected by the Java Runtime Environment: 
# 
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180117a37, pid=6064, tid=0x0000000000002e40 
# 
# JRE version: Java(TM) SE Runtime Environment (8.0_102-b14) (build 1.8.0_102-b14) 
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.102-b14 mixed mode windows-amd64 compressed oops) 
# Problematic frame: 
# C [cygwin1.dll+0xd7a37] 
# 
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows 
# 
# If you would like to submit a bug report, please visit: 
# http://bugreport.java.com/bugreport/crash.jsp 
# The crash happened outside the Java Virtual Machine in native code. 
# See problematic frame for where to report the bug. 
# 

--------------- T H R E A D --------------- 

Current thread (0x0000000002e70800): JavaThread "main" [_thread_in_native, id=11840, stack(0x0000000002d70000,0x0000000002e70000)] 

siginfo: ExceptionCode=0xc0000005, writing address 0x0000000000000000 

Registers: 
RAX=0x0000000000000000, RBX=0x0000000002e6f328, RCX=0x00000003fec8bfe0, RDX=0x00000003fec5ee60 
RSP=0x0000000002e6f100, RBP=0x0000000000000018, RSI=0x0000000002e6f258, RDI=0x0000000002f7835d 
R8 =0x0000000002e6f298, R9 =0x0000000002d67610, R10=0x0000000002e70000, R11=0x00000003fec5ee08 
R12=0x0000000000000000, R13=0x000000001bf903f0, R14=0x0000000002e6f420, R15=0x0000000002e70800 
RIP=0x0000000180117a37, EFLAGS=0x0000000000010202 

Top of Stack: (sp=0x0000000002e6f100) 
0x0000000002e6f100: 00000001800f0390 0000000180117a4b 
0x0000000002e6f110: 0000000002d6eee0 0000000002d6eee8 
0x0000000002e6f120: 0000000002e70800 000000005b94a34e 
0x0000000002e6f130: 0000000002e6f328 00000003fec62d16 
0x0000000002e6f140: 000000005bd50088 0000005000000000 
0x0000000002e6f150: 0000000000000015 0000000000000000 
0x0000000002e6f160: 0000000002e6f1f0 0000000002e6f258 
0x0000000002e6f170: 0000000002f7835d 00000003fec58d59 
0x0000000002e6f180: 0000000000000000 0000000000000100 
0x0000000002e6f190: 0000000000000002 0000000002d6ebf0 
0x0000000002e6f1a0: 0000000002e70800 000000001bf903f0 
0x0000000002e6f1b0: 0000000020200021 0000000002f7835d 
0x0000000002e6f1c0: 0000000002e6f250 00000005c9f710e4 
0x0000000002e6f1d0: 0000000000000010 0000000000000008 
0x0000000002e6f1e0: 0000000002e70800 0000000002f93b63 
0x0000000002e6f1f0: 000000001bf903f0 0000000002e70800 

Instructions: (pc=0x0000000180117a37) 
0x0000000180117a17: 04 f3 90 eb e3 48 c7 c0 08 00 00 00 49 0f c1 82 
0x0000000180117a27: 50 e4 ff ff 4c 8d 1d 19 00 00 00 4c 87 5c 24 08 
0x0000000180117a37: 4c 89 18 41 ff 82 44 e4 ff ff 41 ff 8a 4c e4 ff 
0x0000000180117a47: ff 58 ff e0 65 4c 8b 14 25 08 00 00 00 41 bb 01 


Register to memory mapping: 

RAX=0x0000000000000000 is an unknown value 
RBX=0x0000000002e6f328 is pointing into the stack for thread: 0x0000000002e70800 
RCX=0x00000003fec8bfe0 is an unknown value 
RDX=0x00000003fec5ee60 is an unknown value 
RSP=0x0000000002e6f100 is pointing into the stack for thread: 0x0000000002e70800 
RBP=0x0000000000000018 is an unknown value 
RSI=0x0000000002e6f258 is pointing into the stack for thread: 0x0000000002e70800 
RDI=0x0000000002f7835d is at code_begin+2301 in an Interpreter codelet 
invoke return entry points [0x0000000002f77a60, 0x0000000002f78440] 2528 bytes 
R8 =0x0000000002e6f298 is pointing into the stack for thread: 0x0000000002e70800 
R9 =0x0000000002d67610 is an unknown value 
R10=0x0000000002e70000 is pointing into the stack for thread: 0x0000000002e70800 
R11=0x00000003fec5ee08 is an unknown value 
R12=0x0000000000000000 is an unknown value 
R13={method} {0x000000001bf903f8} 'nativePrint' '()Ljava/lang/String;' in 'Main' 
R14=0x0000000002e6f420 is pointing into the stack for thread: 0x0000000002e70800 
R15=0x0000000002e70800 is a thread 


Stack: [0x0000000002d70000,0x0000000002e70000], sp=0x0000000002e6f100, free space=1020k 
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) 
C [cygwin1.dll+0xd7a37] 
C [cygwin1.dll+0xb038f] 

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code) 
j Main.nativePrint()Ljava/lang/String;+0 
j Main.main([Ljava/lang/String;)V+18 
v ~StubRoutines::call_stub 

--------------- P R O C E S S --------------- 

Java Threads: (=> current thread) 
    0x000000001dea8800 JavaThread "Service Thread" daemon [_thread_blocked, id=3408, stack(0x000000001e280000,0x000000001e380000)] 
    0x000000001c538800 JavaThread "C1 CompilerThread2" daemon [_thread_blocked, id=5372, stack(0x000000001dd80000,0x000000001de80000)] 
    0x000000001c535800 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=5400, stack(0x000000001dc80000,0x000000001dd80000)] 
    0x000000001c531000 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=5124, stack(0x000000001db80000,0x000000001dc80000)] 
    0x000000001c52f000 JavaThread "Attach Listener" daemon [_thread_blocked, id=2432, stack(0x000000001da80000,0x000000001db80000)] 
    0x000000001c52e000 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=336, stack(0x000000001d980000,0x000000001da80000)] 
    0x0000000002f6a800 JavaThread "Finalizer" daemon [_thread_blocked, id=11892, stack(0x000000001d7a0000,0x000000001d8a0000)] 
    0x0000000002f61000 JavaThread "Reference Handler" daemon [_thread_blocked, id=11932, stack(0x000000001d6a0000,0x000000001d7a0000)] 
=>0x0000000002e70800 JavaThread "main" [_thread_in_native, id=11840, stack(0x0000000002d70000,0x0000000002e70000)] 

Other Threads: 
    0x000000001c4f8000 VMThread [stack: 0x000000001d5a0000,0x000000001d6a0000] [id=13280] 
    0x000000001ded3000 WatcherThread [stack: 0x000000001e380000,0x000000001e480000] [id=11656] 

VM state:not at safepoint (normal execution) 

VM Mutex/Monitor currently owned by a thread: None 

Heap: 
PSYoungGen  total 76288K, used 2621K [0x000000076b500000, 0x0000000770a00000, 0x00000007c0000000) 
    eden space 65536K, 4% used [0x000000076b500000,0x000000076b78f5e8,0x000000076f500000) 
    from space 10752K, 0% used [0x000000076ff80000,0x000000076ff80000,0x0000000770a00000) 
    to space 10752K, 0% used [0x000000076f500000,0x000000076f500000,0x000000076ff80000) 
ParOldGen  total 175104K, used 0K [0x00000006c1e00000, 0x00000006cc900000, 0x000000076b500000) 
    object space 175104K, 0% used [0x00000006c1e00000,0x00000006c1e00000,0x00000006cc900000) 
Metaspace  used 2586K, capacity 4486K, committed 4864K, reserved 1056768K 
    class space used 282K, capacity 386K, committed 512K, reserved 1048576K 

Card table byte_map: [0x0000000,0x0000000012b30000] byte_map_base: 0x000000000ed21000 

Marking Bits: (ParMarkBitMap*) 0x000000005bf8a6c0 
Begin Bits: [0x0000000013480000, 0x0000000017408000) 
End Bits: [0x0000000017408000, 0x000000001b390000) 

Polling page: 0x0000000002c00000 

CodeCache: size=245760Kb used=1102Kb max_used=1105Kb free=244657Kb 
bounds [0x0000000002f70000, 0x00000000031e0000, 0x0000000011f70000] 
total_blobs=254 nmethods=23 adapters=145 
compilation: enabled 

Compilation events (10 events): 
Event: 0.060 Thread 0x000000001c538800 18 s 3  java.lang.StringBuffer::append (13 bytes) 
Event: 0.060 Thread 0x000000001c535800 20  4  java.lang.String::charAt (29 bytes) 
Event: 0.060 Thread 0x000000001c538800 nmethod 18 0x0000000003080210 code [0x00000000030803c0, 0x0000000003080af8] 
Event: 0.060 Thread 0x000000001c538800 21  3  java.lang.AbstractStringBuilder::append (50 bytes) 
Event: 0.061 Thread 0x000000001c538800 nmethod 21 0x0000000003080ed0 code [0x00000000030810a0, 0x00000000030817f8] 
Event: 0.061 Thread 0x000000001c538800 22  3  java.lang.String::indexOf (7 bytes) 
Event: 0.061 Thread 0x000000001c535800 nmethod 20 0x00000000030847d0 code [0x0000000003084920, 0x00000000030849f8] 
Event: 0.061 Thread 0x000000001c538800 nmethod 22 0x00000000030843d0 code [0x0000000003084540, 0x0000000003084748] 
Event: 0.064 Thread 0x000000001c538800 23  3  java.util.Arrays::copyOfRange (63 bytes) 
Event: 0.064 Thread 0x000000001c538800 nmethod 23 0x0000000003082950 code [0x0000000003082b60, 0x0000000003083698] 

GC Heap History (0 events): 
No events 

Deoptimization events (0 events): 
No events 

Internal exceptions (2 events): 
Event: 0.025 Thread 0x0000000002e70800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x000000076b507c78) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u102\7268\hotspot\s�4�B��? 
Event: 0.025 Thread 0x0000000002e70800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x000000076b507f60) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u102\7268\hotspot\src\share\vm\prims\ 

Events (10 events): 
Event: 0.063 loading class java/security/UnresolvedPermission 
Event: 0.063 loading class java/security/UnresolvedPermission done 
Event: 0.064 loading class java/security/BasicPermissionCollection 
Event: 0.064 loading class java/security/BasicPermissionCollection done 
Event: 0.064 loading class sun/launcher/LauncherHelper$FXHelper 
Event: 0.064 loading class sun/launcher/LauncherHelper$FXHelper done 
Event: 0.064 loading class java/lang/Class$MethodArray 
Event: 0.064 loading class java/lang/Class$MethodArray done 
Event: 0.064 loading class java/lang/Void 
Event: 0.064 loading class java/lang/Void done 


Dynamic libraries: 
0x00007ff77e020000 - 0x00007ff77e057000  C:\Program Files\Java\jdk1.8.0_102\bin\java.exe 
0x00007ffb13b20000 - 0x00007ffb13ce1000  C:\Windows\SYSTEM32\ntdll.dll 
0x00007ffb13110000 - 0x00007ffb131bd000  C:\Windows\system32\KERNEL32.DLL 
0x00007ffb10220000 - 0x00007ffb10408000  C:\Windows\system32\KERNELBASE.dll 
0x00007ffb13910000 - 0x00007ffb139b7000  C:\Windows\system32\ADVAPI32.dll 
0x00007ffb13870000 - 0x00007ffb1390d000  C:\Windows\system32\msvcrt.dll 
0x00007ffb12d30000 - 0x00007ffb12d8b000  C:\Windows\system32\sechost.dll 
0x00007ffb12f80000 - 0x00007ffb1309c000  C:\Windows\system32\RPCRT4.dll 
0x00007ffb13390000 - 0x00007ffb134e6000  C:\Windows\system32\USER32.dll 
0x00007ffb12df0000 - 0x00007ffb12f76000  C:\Windows\system32\GDI32.dll 
0x00007ffb0bbb0000 - 0x00007ffb0be24000  C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.10586.589_none_a2ddb3caa539acce\COMCTL32.dll 
0x00007ffb10ff0000 - 0x00007ffb1126d000  C:\Windows\system32\combase.dll 
0x00007ffb10600000 - 0x00007ffb1066a000  C:\Windows\system32\bcryptPrimitives.dll 
0x00007ffb13ad0000 - 0x00007ffb13b0b000  C:\Windows\system32\IMM32.DLL 
0x000000005b690000 - 0x000000005b761000  C:\Program Files\Java\jdk1.8.0_102\jre\bin\msvcr100.dll 
0x000000005b770000 - 0x000000005c00a000  C:\Program Files\Java\jdk1.8.0_102\jre\bin\server\jvm.dll 
0x00007ffb13b10000 - 0x00007ffb13b18000  C:\Windows\system32\PSAPI.DLL 
0x00007ffb0bee0000 - 0x00007ffb0beea000  C:\Windows\SYSTEM32\VERSION.dll 
0x00007ffb0cf30000 - 0x00007ffb0cf39000  C:\Windows\SYSTEM32\WSOCK32.dll 
0x00007ffb0e7e0000 - 0x00007ffb0e803000  C:\Windows\SYSTEM32\WINMM.dll 
0x00007ffb134f0000 - 0x00007ffb1355b000  C:\Windows\system32\WS2_32.dll 
0x0000000002c40000 - 0x0000000002c6c000  C:\Windows\SYSTEM32\WINMMBASE.dll 
0x00007ffb101d0000 - 0x00007ffb10213000  C:\Windows\system32\cfgmgr32.dll 
0x000000005cd10000 - 0x000000005cd1f000  C:\Program Files\Java\jdk1.8.0_102\jre\bin\verify.dll 
0x000000005cad0000 - 0x000000005caf9000  C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.dll 
0x000000005ccf0000 - 0x000000005cd06000  C:\Program Files\Java\jdk1.8.0_102\jre\bin\zip.dll 
0x00007ffb116c0000 - 0x00007ffb12c1c000  C:\Windows\system32\SHELL32.dll 
0x00007ffb10840000 - 0x00007ffb10e85000  C:\Windows\system32\windows.storage.dll 
0x00007ffb13330000 - 0x00007ffb13382000  C:\Windows\system32\shlwapi.dll 
0x00007ffb10170000 - 0x00007ffb1017f000  C:\Windows\system32\kernel.appcore.dll 
0x00007ffb10430000 - 0x00007ffb104e5000  C:\Windows\system32\shcore.dll 
0x00007ffb10180000 - 0x00007ffb101cb000  C:\Windows\system32\powrprof.dll 
0x00007ffb10140000 - 0x00007ffb10154000  C:\Windows\system32\profapi.dll 
0x00000005c9f70000 - 0x00000005c9f88000  C:\Users\Moe\Desktop\Projects\IntelligentCloud\IntelligentCloudLibrary\build\libs\main\shared\main.dll 
0x00000003ff750000 - 0x00000003ff76a000  C:\cygwin64\bin\cyggcc_s-seh-1.dll 
0x00000003febd0000 - 0x00000003fed22000  C:\cygwin64\bin\cygstdc++-6.dll 
0x0000000180040000 - 0x0000000180610000  C:\cygwin64\bin\cygwin1.dll 
0x00007ffb0cb00000 - 0x00007ffb0cc8c000  C:\Windows\SYSTEM32\dbghelp.dll 

VM Arguments: 
jvm_args: -Dfile.encoding=windows-1252 -Duser.country=US -Duser.language=en -Duser.variant 
java_command: Main 
java_class_path (initial): C:\Users\Moe\Desktop\Projects\IntelligentCloud\IntelligentCloudApp\build\classes\main;C:\Users\Moe\Desktop\Projects\IntelligentCloud\IntelligentCloudApp\build\resources\main 
Launcher Type: SUN_STANDARD 

Environment Variables: 
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_102 
PATH=C:\cygwin64\bin;C:\\PROGRA~1\\rti_connext_dds-5.2.3\;C:\\PROGRA~1\\rti_connext_dds-5.2.3\\bin;C:\Program Files\ArrayFire\v3\lib;C:\Program Files\Java\jdk1.8.0_102\bin 
USERNAME=Moe 
OS=Windows_NT 
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 94 Stepping 3, GenuineIntel 



--------------- S Y S T E M --------------- 

OS: Windows 10.0 , 64 bit Build 10586 (10.0.10586.589) 

CPU:total 4 (4 cores per cpu, 1 threads per core) family 6 model 94 stepping 3, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, avx, avx2, aes, clmul, erms, rtm, 3dnowpref, lzcnt, tsc, tscinvbit, bmi1, bmi2, adx 

Memory: 4k page, physical 16647436k(9117744k free), swap 19137804k(9821056k free) 

vm_info: Java HotSpot(TM) 64-Bit Server VM (25.102-b14) for windows-amd64 JRE (1.8.0_102-b14), built on Jun 22 2016 13:15:21 by "java_re" with MS VC++ 10.0 (VS2010) 

time: Mon Sep 26 13:31:32 2016 
elapsed time: 0 seconds (0d 0h 0m 0s) 
+0

你是如何構建你的JNI庫的? MSVC?轉儲信息在哪裏? – rustyx

+0

@RustyX當我運行應用程序時,它現在似乎掛起來了,但我發佈了一些在它崩潰前輸出的轉儲文件。它應該包含所有的信息。 –

+0

使用靜態或動態鏈接的C++庫,使用g ++編譯您的本地庫嗎?如果是後者,你是從Java代碼中加載它的嗎? –

回答

2

@RustyX很可能是正確的。即使在單個進程中混合使用多個C++運行時庫,如果可能的話也不太可能奏效。

JVM似乎依賴於Microsoft C++運行時。添加GNU C++運行時不太可行。這從MinGW的網頁地址的一些問題:

Interoperability of Libraries Created by Different Compiler Brands

...

即使GNU G ++現在可以鏈接MSVC C++庫,並能產生 MSVC++兼容庫/ DLL文件,這並不意味着它們將會是 由於C++的動態特性而能夠在運行時工作。一些 可能的原因是:

  • 簡單的名稱修復問題,它可以用一個明確的.def文件繞過。
  • 需要正確編譯器選項的不同結構對齊問題(-mms-bitfields,...)。
  • 底層異常和內存模型的基本衝突:
    • 新的/刪除或的malloc /免費的MSVC DLL不會與合作,Cygwin的newlib新/刪除或的malloc /免費。不能釋放空間 ,這個空間在使用不同的new/malloc的函數中被分配。
    • 由MSVC DLL引發的異常不會被Cygwin可執行文件捕獲,反之亦然。
    • 緩慢的GNU SJLJ異常模型(在GCC-3中使用)。x和更早的版本)與MSVC++模型兼容,但新的DWARF2模型(其中 將被GCC-4.x使用)將不兼容。

解決存儲器管理兼容性問題和不同new/deletemalloc/free實現之間的衝突將是困難的,如果不是不可能的。

+1

從Cygwin切換到MiniGW已經解決了我所有的問題! –