gpt4 book ai didi

java - 带有 C++ 的 JNI 在 cout、printf 和方法调用上不断崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:21:56 26 4
gpt4 key购买 nike

我正在用 C++ 编写我的第一个 JNI 程序,并制作了一些测试方法以确保一切正常。

问题是:当我调用方法测试时,它给出的结果波动很大。有时它会永远运行,有时我会遇到构建错误,有时我会收到 fatal error 和错误消息。这不仅发生在 printf 和 std::cout 上,而且发生在方法调用上(然后我总是遇到 fatal error 。我没有在问题中包含一个例子)。我看不到错误。希望得到一些帮助。

我目前使用 g++ 作为编译器,还有其他选项:-D__int64=int64_t。

Java 类:

public class JNI {
public static native int test();

public static native int test1(int a);

public static void main(String[] args) {
String filename = "Path-to-lib\\libProjectJNI.dll";
System.load(filename);

//Prints 10 - every time - as expected
System.out.println(test1(5));

//Hangs, build fails, or fatal error every time.
System.out.println(test());
}
}

C++ 类:

#include "JNI.h"
#include <iostream>
#include <string>

JNIEXPORT jint JNICALL Java_project_JNI_test
(JNIEnv * env, jclass obj) {
std::cout << "PRINT THIS";
return 10;
}

JNIEXPORT jint JNICALL Java_project_JNI_test1
(JNIEnv *env, jclass obj, jint aint) {
return aint * 2;
}

C++ h:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class project_JNI */

#ifndef _Included_project_JNI
#define _Included_project_JNI
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_project_JNI_test
(JNIEnv *, jclass);


JNIEXPORT jint JNICALL Java_project_JNI_test1
(JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif

构建时出现的错误:

\nbproject\build-impl.xml:1051: The following error occurred while executing this line:
\nbproject\build-impl.xml:805: Java returned: -1073741819
BUILD FAILED (total time: 0 seconds)

hs_err_pid### 日志

#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (0x20474343), pid=33944, tid=3260
#
# JRE version: Java(TM) SE Runtime Environment (8.0_60-b27) (build 1.8.0_60-b27)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.60-b23 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [KERNELBASE.dll+0x71f28]
#
# 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 (0x0000000002260800): JavaThread "main" [_thread_in_native, id=3260, stack(0x0000000002110000,0x0000000002210000)]

siginfo: ExceptionCode=0x20474343, ExceptionInformation=0x0000000600000070

Registers:
RAX=0x0000000000000000, RBX=0x0000000600000070, RCX=0x0000000000000000, RDX=0x0000000000000000
RSP=0x000000000220f460, RBP=0x000000000220f730, RSI=0x00000003fefac4f0, RDI=0x00000006000000b0
R8 =0x0000000000000000, R9 =0x0000000000000000, R10=0x0000000000000000, R11=0x0000000000000000
R12=0x0000000000000000, R13=0x000000000000000a, R14=0x000000000220f670, R15=0x00000005696e3030
RIP=0x00007ffb434f1f28, EFLAGS=0x0000000000000206

Top of Stack: (sp=0x000000000220f460)
0x000000000220f460: 00000000ffffffff 0000000600000070
0x000000000220f470: 00000003fefac4f0 00000006000000b0
0x000000000220f480: 0000000020474343 0000000000000000
0x000000000220f490: 00007ffb434f1f28 0000000600000001
0x000000000220f4a0: 0000000600000070 000000000000000a
0x000000000220f4b0: 0000000600000300 00000001800ce32c
0x000000000220f4c0: 00000001801523c1 000000000220f480
0x000000000220f4d0: 00000003fefbfec0 0000000180156860
0x000000000220f4e0: 00000006000001e0 0000000000000001
0x000000000220f4f0: 00000000000000b0 000000000220f730
0x000000000220f500: 0000000000000000 0000000000000000
0x000000000220f510: 0000000000000008 00000003ffa4d5ea
0x000000000220f520: 000077d3bf56b135 00000003fefbfec0
0x000000000220f530: 000000000220f4e0 00000003ffa4cc97
0x000000000220f540: 00000006000000b0 00000003fefac4f0
0x000000000220f550: 00000003fefcbcd0 000000000220f730

Instructions: (pc=0x00007ffb434f1f28)
0x00007ffb434f1f08: 49 8b d1 44 0f 47 c0 44 89 44 24 38 49 c1 e0 03
0x00007ffb434f1f18: e8 27 b2 00 00 48 8d 4c 24 20 ff 15 c8 a7 05 00
0x00007ffb434f1f28: 48 8b 8c 24 c0 00 00 00 48 33 cc e8 98 7a 00 00
0x00007ffb434f1f38: 48 81 c4 d8 00 00 00 c3 83 64 24 38 00 eb d6 cc


Register to memory mapping:

RAX=0x0000000000000000 is an unknown value
RBX=0x0000000600000070 is an unknown value
RCX=0x0000000000000000 is an unknown value
RDX=0x0000000000000000 is an unknown value
RSP=0x000000000220f460 is pointing into the stack for thread: 0x0000000002260800
RBP=0x000000000220f730 is pointing into the stack for thread: 0x0000000002260800
RSI=0x00000003fefac4f0 is an unknown value
RDI=0x00000006000000b0 is an unknown value
R8 =0x0000000000000000 is an unknown value
R9 =0x0000000000000000 is an unknown value
R10=0x0000000000000000 is an unknown value
R11=0x0000000000000000 is an unknown value
R12=0x0000000000000000 is an unknown value
R13=0x000000000000000a is an unknown value
R14=0x000000000220f670 is pointing into the stack for thread: 0x0000000002260800
R15=0x00000005696e3030 is an unknown value


Stack: [0x0000000002110000,0x0000000002210000], sp=0x000000000220f460, free space=1021k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [KERNELBASE.dll+0x71f28]
C [cyggcc_s-seh-1.dll+0xcc97]
C [cygstdc++-6.dll+0xba6d1]
C [cygstdc++-6.dll+0xb0a7f]
C [cygstdc++-6.dll+0xada61]
C [cygstdc++-6.dll+0xaff59]
C [cygstdc++-6.dll+0xb4ee7]
C [libProjectJNI.dll+0x10b3]
C 0x0000000002375e34

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j gol.s305089.JNI.test()I+0
j gol.s305089.JNI.main([Ljava/lang/String;)V+20
v ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
0x00000000187fc800 JavaThread "Service Thread" daemon [_thread_blocked, id=2792, stack(0x0000000018bd0000,0x0000000018cd0000)]
0x0000000016e97000 JavaThread "C1 CompilerThread2" daemon [_thread_blocked, id=35452, stack(0x00000000186d0000,0x00000000187d0000)]
0x0000000016e8f800 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=15496, stack(0x00000000185d0000,0x00000000186d0000)]
0x0000000016e8d800 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=17716, stack(0x00000000184d0000,0x00000000185d0000)]
0x0000000016e8b000 JavaThread "Attach Listener" daemon [_thread_blocked, id=26948, stack(0x00000000183d0000,0x00000000184d0000)]
0x0000000016e89800 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=9012, stack(0x00000000182d0000,0x00000000183d0000)]
0x0000000002356800 JavaThread "Finalizer" daemon [_thread_blocked, id=24532, stack(0x00000000180e0000,0x00000000181e0000)]
0x000000000234d800 JavaThread "Reference Handler" daemon [_thread_blocked, id=35052, stack(0x0000000017fe0000,0x00000000180e0000)]
=>0x0000000002260800 JavaThread "main" [_thread_in_native, id=3260, stack(0x0000000002110000,0x0000000002210000)]

Other Threads:
0x0000000016e58000 VMThread [stack: 0x0000000017ee0000,0x0000000017fe0000] [id=4028]
0x0000000018803000 WatcherThread [stack: 0x0000000018cd0000,0x0000000018dd0000] [id=7356]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap:
PSYoungGen total 38400K, used 1996K [0x00000000d5f00000, 0x00000000d8980000, 0x0000000100000000)
eden space 33280K, 6% used [0x00000000d5f00000,0x00000000d60f33f8,0x00000000d7f80000)
from space 5120K, 0% used [0x00000000d8480000,0x00000000d8480000,0x00000000d8980000)
to space 5120K, 0% used [0x00000000d7f80000,0x00000000d7f80000,0x00000000d8480000)
ParOldGen total 87552K, used 0K [0x0000000081c00000, 0x0000000087180000, 0x00000000d5f00000)
object space 87552K, 0% used [0x0000000081c00000,0x0000000081c00000,0x0000000087180000)
Metaspace used 2783K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 302K, capacity 386K, committed 512K, reserved 1048576K

Card table byte_map: [0x0000000011720000,0x0000000011b20000] byte_map_base: 0x0000000011312000

Marking Bits: (ParMarkBitMap*) 0x0000000070aca5b0
Begin Bits: [0x00000000121d0000, 0x0000000014160000)
End Bits: [0x0000000014160000, 0x00000000160f0000)

Polling page: 0x0000000000910000

CodeCache: size=245760Kb used=1102Kb max_used=1102Kb free=244657Kb
bounds [0x0000000002360000, 0x00000000025d0000, 0x0000000011360000]
total_blobs=260 nmethods=26 adapters=148
compilation: enabled

Compilation events (10 events):
Event: 0.079 Thread 0x0000000016e97000 22 3 java.util.jar.Attributes$Name::isAlpha (30 bytes)
Event: 0.079 Thread 0x0000000016e97000 nmethod 22 0x000000000246fb50 code [0x000000000246fcc0, 0x000000000246fef0]
Event: 0.080 Thread 0x0000000016e97000 23 3 sun.misc.ASCIICaseInsensitiveComparator::toLower (16 bytes)
Event: 0.080 Thread 0x0000000016e97000 nmethod 23 0x0000000002472950 code [0x0000000002472ac0, 0x0000000002472d10]
Event: 0.080 Thread 0x0000000016e97000 24 3 sun.misc.ASCIICaseInsensitiveComparator::isUpper (18 bytes)
Event: 0.080 Thread 0x0000000016e97000 nmethod 24 0x0000000002472dd0 code [0x0000000002472f20, 0x00000000024730d0]
Event: 0.080 Thread 0x0000000016e97000 25 3 java.lang.String::getChars (62 bytes)
Event: 0.081 Thread 0x0000000016e97000 nmethod 25 0x0000000002473150 code [0x0000000002473300, 0x0000000002473738]
Event: 0.082 Thread 0x0000000016e97000 26 3 java.lang.System::getSecurityManager (4 bytes)
Event: 0.082 Thread 0x0000000016e97000 nmethod 26 0x0000000002473950 code [0x0000000002473aa0, 0x0000000002473bd0]

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Internal exceptions (2 events):
Event: 0.028 Thread 0x0000000002260800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x00000000d5f0da00) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u60\4407\hotspot\srÃö×O ?
Event: 0.028 Thread 0x0000000002260800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x00000000d5f0dce8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u60\4407\hotspot\src\share\vm\prims\j

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


Dynamic libraries:
0x00007ff69b060000 - 0x00007ff69b097000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\java.exe
0x00007ffb465f0000 - 0x00007ffb467b1000 C:\WINDOWS\SYSTEM32\ntdll.dll
0x00007ffb45170000 - 0x00007ffb4521d000 C:\WINDOWS\system32\KERNEL32.DLL
0x00007ffb43480000 - 0x00007ffb43668000 C:\WINDOWS\system32\KERNELBASE.dll
0x00007ffb45850000 - 0x00007ffb458f7000 C:\WINDOWS\system32\ADVAPI32.dll
0x00007ffb452f0000 - 0x00007ffb4538d000 C:\WINDOWS\system32\msvcrt.dll
0x00007ffb45530000 - 0x00007ffb4558b000 C:\WINDOWS\system32\sechost.dll
0x00007ffb46020000 - 0x00007ffb4613c000 C:\WINDOWS\system32\RPCRT4.dll
0x00007ffb45e60000 - 0x00007ffb45fb6000 C:\WINDOWS\system32\USER32.dll
0x00007ffb45390000 - 0x00007ffb45516000 C:\WINDOWS\system32\GDI32.dll
0x00007ffb3e860000 - 0x00007ffb3ead4000 C:\WINDOWS\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.10586.0_none_8c15ae12515e1c22\COMCTL32.dll
0x00007ffb45a20000 - 0x00007ffb45c9d000 C:\WINDOWS\system32\combase.dll
0x00007ffb43840000 - 0x00007ffb438aa000 C:\WINDOWS\system32\bcryptPrimitives.dll
0x00007ffb45fe0000 - 0x00007ffb4601b000 C:\WINDOWS\system32\IMM32.DLL
0x00000000726a0000 - 0x0000000072772000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\msvcr100.dll
0x00000000702c0000 - 0x0000000070b49000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\server\jvm.dll
0x00007ffb45520000 - 0x00007ffb45528000 C:\WINDOWS\system32\PSAPI.DLL
0x00007ffb40550000 - 0x00007ffb40573000 C:\WINDOWS\SYSTEM32\WINMM.dll
0x00007ffb37a00000 - 0x00007ffb37a09000 C:\WINDOWS\SYSTEM32\WSOCK32.dll
0x00007ffb412a0000 - 0x00007ffb412aa000 C:\WINDOWS\SYSTEM32\VERSION.dll
0x00007ffb459b0000 - 0x00007ffb45a1b000 C:\WINDOWS\system32\WS2_32.dll
0x00007ffb404f0000 - 0x00007ffb4051c000 C:\WINDOWS\SYSTEM32\WINMMBASE.dll
0x00007ffb42de0000 - 0x00007ffb42e23000 C:\WINDOWS\system32\cfgmgr32.dll
0x0000000072650000 - 0x000000007265f000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\verify.dll
0x0000000072620000 - 0x0000000072649000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\java.dll
0x0000000072600000 - 0x0000000072616000 C:\Program Files\Java\jdk1.8.0_60\jre\bin\zip.dll
0x00007ffb439f0000 - 0x00007ffb44f4c000 C:\WINDOWS\system32\SHELL32.dll
0x00007ffb42e30000 - 0x00007ffb43475000 C:\WINDOWS\system32\windows.storage.dll
0x00007ffb46140000 - 0x00007ffb46192000 C:\WINDOWS\system32\shlwapi.dll
0x00007ffb42c60000 - 0x00007ffb42c6f000 C:\WINDOWS\system32\kernel.appcore.dll
0x00007ffb42d20000 - 0x00007ffb42dd5000 C:\WINDOWS\system32\shcore.dll
0x00007ffb42c10000 - 0x00007ffb42c5b000 C:\WINDOWS\system32\powrprof.dll
0x00007ffb42c70000 - 0x00007ffb42c84000 C:\WINDOWS\system32\profapi.dll
0x00000005696e0000 - 0x0000000569704000 C:\Users\active-folder-jni\dist\Debug\Cygwin-Windows\libProjectJNI.dll
0x0000000180040000 - 0x0000000180640000 C:\cygwin64\bin\cygwin1.dll
0x00000003fef00000 - 0x00000003ff055000 C:\cygwin64\bin\cygstdc++-6.dll
0x00000003ffa40000 - 0x00000003ffa5a000 C:\cygwin64\bin\cyggcc_s-seh-1.dll
0x00007ffb2c030000 - 0x00007ffb2c1bc000 C:\WINDOWS\SYSTEM32\dbghelp.dll

VM Arguments:
jvm_args: -Dfile.encoding=UTF-8
java_command: gol.s305089.JNI
java_class_path (initial): C:\Users\active-folder-java-src\dist\Project.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\plugin.jar;C:\Users\active-folder-java-src\build\classes
Launcher Type: SUN_STANDARD

Environment Variables:
PATH=C:\Program Files (x86)\iis express\PHP\v5.3;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\MATLAB\R2015b\runtime\win64;C:\Program Files\MATLAB\R2015b\bin;C:\Program Files\Git\cmd;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\cygwin64\bin;C:\Program Files\Java\jdk1.8.0_60\bin
USERNAME=myUsername
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 60 Stepping 3, GenuineIntel



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

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

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

Memory: 4k page, physical 8268824k(3297764k free), swap 9579544k(2510856k free)

vm_info: Java HotSpot(TM) 64-Bit Server VM (25.60-b23) for windows-amd64 JRE (1.8.0_60-b27), built on Aug 4 2015 11:06:27 by "java_re" with MS VC++ 10.0 (VS2010)

time: Sat Apr 30 21:52:33 2016
elapsed time: 0 seconds (0d 0h 0m 0s)

最佳答案

是否像 C++ 端给出一个 8 字节整数地址一样简单,但 Java 认为您接收到的 int 类型是一个 4 字节整数?

Java 8 SDK 注释说 int 是 32 位。 https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html

由于您从 C++ 返回一个(可能是 64 位)整数,然后 Java 将其“转换”为 32 位类型。尝试将 Java 端的返回类型更改为 long 并查看它是否有效。

 public static native long test1(int a);

此外,为什么不尝试以下操作,看看您是否可以判断崩溃发生的位置?

  long n = test1(5);  // get the value into a 64 bit int
System.out.format("%d%n", n); // print it separately

关于java - 带有 C++ 的 JNI 在 cout、printf 和方法调用上不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36959657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com