执行任何 IO/流时 C++ JNI 崩溃/挂起

C++ JNI crashes/hangs when doing any IO/Streams(执行任何 IO/流时 C++ JNI 崩溃/挂起)

本文介绍了执行任何 IO/流时 C++ JNI 崩溃/挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试编译 C++ 代码以在 JAVA 端使用时,我似乎遇到了一个大问题.当我尝试做任何与 IO/Stream/Manipulation 相关的事情时,JVM 似乎崩溃了.

I seem to have a big problem when trying to compile C++ code to use on the JAVA side. When I try to do anything IO/Stream/Manipulation related, the JVM seems to crash.

有时,就像我每 10 次运行应用程序时一样,JNI 100% 地处理所有 IO 内容.然后其他时间 JVM 崩溃或挂起.

Sometimes, like once every 10 times when I run the application, the JNI works 100% with all the IO stuff. Then the rest of the times the JVM crashes or hangs.

我正在使用:Windows 10 x64、JAVA jdk1.8.0_102、Netbeans 8.1、g++,其他选项:-D__int64=int64_t

当我这样做时,JNI 工作正常:

When I do these, the JNI works fine:

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::stringstreamsprintfcoutputs,JVM 崩溃.即使实例化 std::stringstream 似乎让我崩溃.我假设其他类型的操作函数也会让我崩溃:

When I try incorporating anything such as printf, std::stringstream, sprintf, cout, or puts, the JVM crashes. Even instantiating the std::stringstream seems to crash me. I assume other sorts of manipulation function also crashes me:

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");
}

我似乎和这个人有类似的问题:

I seem to be having a similar problem to this person:

使用 c++ 的 JNI 在 cout 上不断崩溃, printf 和方法调用

这是我的转储:

#
# 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: [0x0000000012330000,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:
eworkspace8-2-build-windows-amd64-cygwinjdk8u1027268hotspots�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:
eworkspace8-2-build-windows-amd64-cygwinjdk8u1027268hotspotsrcsharevmprims

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 FilesJavajdk1.8.0_102injava.exe
0x00007ffb13b20000 - 0x00007ffb13ce1000     C:WindowsSYSTEM32
tdll.dll
0x00007ffb13110000 - 0x00007ffb131bd000     C:Windowssystem32KERNEL32.DLL
0x00007ffb10220000 - 0x00007ffb10408000     C:Windowssystem32KERNELBASE.dll
0x00007ffb13910000 - 0x00007ffb139b7000     C:Windowssystem32ADVAPI32.dll
0x00007ffb13870000 - 0x00007ffb1390d000     C:Windowssystem32msvcrt.dll
0x00007ffb12d30000 - 0x00007ffb12d8b000     C:Windowssystem32sechost.dll
0x00007ffb12f80000 - 0x00007ffb1309c000     C:Windowssystem32RPCRT4.dll
0x00007ffb13390000 - 0x00007ffb134e6000     C:Windowssystem32USER32.dll
0x00007ffb12df0000 - 0x00007ffb12f76000     C:Windowssystem32GDI32.dll
0x00007ffb0bbb0000 - 0x00007ffb0be24000     C:WindowsWinSxSamd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.10586.589_none_a2ddb3caa539acceCOMCTL32.dll
0x00007ffb10ff0000 - 0x00007ffb1126d000     C:Windowssystem32combase.dll
0x00007ffb10600000 - 0x00007ffb1066a000     C:Windowssystem32cryptPrimitives.dll
0x00007ffb13ad0000 - 0x00007ffb13b0b000     C:Windowssystem32IMM32.DLL
0x000000005b690000 - 0x000000005b761000     C:Program FilesJavajdk1.8.0_102jreinmsvcr100.dll
0x000000005b770000 - 0x000000005c00a000     C:Program FilesJavajdk1.8.0_102jreinserverjvm.dll
0x00007ffb13b10000 - 0x00007ffb13b18000     C:Windowssystem32PSAPI.DLL
0x00007ffb0bee0000 - 0x00007ffb0beea000     C:WindowsSYSTEM32VERSION.dll
0x00007ffb0cf30000 - 0x00007ffb0cf39000     C:WindowsSYSTEM32WSOCK32.dll
0x00007ffb0e7e0000 - 0x00007ffb0e803000     C:WindowsSYSTEM32WINMM.dll
0x00007ffb134f0000 - 0x00007ffb1355b000     C:Windowssystem32WS2_32.dll
0x0000000002c40000 - 0x0000000002c6c000     C:WindowsSYSTEM32WINMMBASE.dll
0x00007ffb101d0000 - 0x00007ffb10213000     C:Windowssystem32cfgmgr32.dll
0x000000005cd10000 - 0x000000005cd1f000     C:Program FilesJavajdk1.8.0_102jreinverify.dll
0x000000005cad0000 - 0x000000005caf9000     C:Program FilesJavajdk1.8.0_102jreinjava.dll
0x000000005ccf0000 - 0x000000005cd06000     C:Program FilesJavajdk1.8.0_102jreinzip.dll
0x00007ffb116c0000 - 0x00007ffb12c1c000     C:Windowssystem32SHELL32.dll
0x00007ffb10840000 - 0x00007ffb10e85000     C:Windowssystem32windows.storage.dll
0x00007ffb13330000 - 0x00007ffb13382000     C:Windowssystem32shlwapi.dll
0x00007ffb10170000 - 0x00007ffb1017f000     C:Windowssystem32kernel.appcore.dll
0x00007ffb10430000 - 0x00007ffb104e5000     C:Windowssystem32shcore.dll
0x00007ffb10180000 - 0x00007ffb101cb000     C:Windowssystem32powrprof.dll
0x00007ffb10140000 - 0x00007ffb10154000     C:Windowssystem32profapi.dll
0x00000005c9f70000 - 0x00000005c9f88000     C:UsersMoeDesktopProjectsIntelligentCloudIntelligentCloudLibraryuildlibsmainsharedmain.dll
0x00000003ff750000 - 0x00000003ff76a000     C:cygwin64incyggcc_s-seh-1.dll
0x00000003febd0000 - 0x00000003fed22000     C:cygwin64incygstdc++-6.dll
0x0000000180040000 - 0x0000000180610000     C:cygwin64incygwin1.dll
0x00007ffb0cb00000 - 0x00007ffb0cc8c000     C:WindowsSYSTEM32dbghelp.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:UsersMoeDesktopProjectsIntelligentCloudIntelligentCloudAppuildclassesmain;C:UsersMoeDesktopProjectsIntelligentCloudIntelligentCloudAppuild
esourcesmain
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=C:Program FilesJavajdk1.8.0_102
PATH=C:cygwin64in;C:\PROGRA~1\rti_connext_dds-5.2.3;C:\PROGRA~1\rti_connext_dds-5.2.3\bin;C:Program FilesArrayFirev3lib;C:Program FilesJavajdk1.8.0_102in
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)

推荐答案

@RustyX 可能是正确的.在一个进程中混合多个 C++ 运行时库不太可能很好,如果有的话.

@RustyX is likely correct. Mixing multiple C++ run-time libraries in a single process is not likely to work well, if at all.

JVM 似乎依赖于 Microsoft C++ 运行时.添加 GNU C++ 运行时不太可能起作用.MinGW 的这个页面解决了一些问题:

The JVM appears to rely on the Microsoft C++ run-time. Adding the GNU C++ run-time isn't likely to work. This page from MinGW addresses some of the issues:

不同编译器品牌创建的库的互操作性

...

尽管 GNU g++ 现在可以链接 MSVC C++ 库,并且可以生成MSVC++ 兼容库/DLL,这并不意味着它们将是由于 C++ 的动态特性,能够在运行时工作.一些可能的原因是:

Even though GNU g++ can link MSVC C++ libraries now, and can produce MSVC++ compatible libraries/DLLs, this does not mean that they will be able to work at run-time due to the dynamic nature of C++. Some possible reasons for this are:

  • 使用显式 .def 文件可以规避的简单名称修改问题.
  • 需要正确编译器选项(-mms-bitfields,...)的不同结构对齐问题.
  • 底层异常和内存模型的根本冲突:
    • MSVC DLL 中的 new/delete 或 malloc/free 不会与 Cygwin newlib new/delete 或 malloc/free 协作.无法释放空间它是使用不同的 new/malloc 在函数中分配的.
    • 由 MSVC DLL 引发的异常不会被 Cygwin 可执行文件捕获,反之亦然.
    • 慢速 GNU SJLJ 异常模型(用于 GCC-3.x 及更早版本)与 MSVC++ 模型兼容,但新的 DWARF2 模型(其中将被 GCC-4.x 使用),将不兼容.

    正在解决不同 new/deletemalloc/free 实现之间的内存管理兼容性问题和冲突即使不是不可能也很难.

    Resolving memory management compatibility issues and conflicts between different new/delete and malloc/free implementations is going to be difficult if not impossible.

    这篇关于执行任何 IO/流时 C++ JNI 崩溃/挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:执行任何 IO/流时 C++ JNI 崩溃/挂起

基础教程推荐