Native.free cause crash in callback from C++ dll

103 views
Skip to first unread message

洪工

unread,
Aug 15, 2021, 10:44:02 PM8/15/21
to Java Native Access

My DLL defines a callback function named as mycallback
DLL_API typedef void(mycallback)(void ptr, int userData);
where ptr is a mallocated buf:
ptr = (MyStructure*)malloc(sizeof(MyStructure));

in java, before return from mycallback, it calls Native.free to free the memory:
Native.free(Memory.nativeValue(ptr));

when call Native.free,the vm is crashed.

my dll is a x64 which is developed us msvc 2019. java sdk version is jdk-8u301-windows-x64.

the following is the log file(hs_err_pid16904.log) generated:

A fatal error has been detected by the Java Runtime Environment:EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffae7b26d71, pid=16904, tid=0x0000000000002e38JRE version: Java(TM) SE Runtime Environment (8.0_301-b09) (build 1.8.0_301-b09)Java VM: Java HotSpot(TM) 64-Bit Server VM (25.301-b09 mixed mode windows-amd64 compressed oops)Problematic frame:C [ntdll.dll+0xa6d71]Failed to write core dump. Minidumps are not enabled by default on client versions of WindowsIf you would like to submit a bug report, please visit:http://bugreport.java.com/bugreport/crash.jspThe 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 (0x00000185d7c52000): JavaThread "playerer_cb2" daemon [_thread_in_native, id=11832, stack(0x00000015c4300000,0x00000015c4400000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000000000ac

Registers:
RAX=0x000000006ea739f0, RBX=0x00000185d7beb3e0, RCX=0x000000006e459ff0, RDX=0x0000000000000078
RSP=0x00000015c43fe790, RBP=0x00000185d7e30000, RSI=0x00000185d7c4f070, RDI=0x0000000000000000
R8 =0x0000000000000094, R9 =0x00000000318ef21f, R10=0x00000185bc8b4c7b, R11=0x000000006e67a0a0
R12=0x0000000000000000, R13=0x00000185d7c4f060, R14=0x00000015c43fe978, R15=0x0000000000000000
RIP=0x00007ffae7b26d71, EFLAGS=0x0000000000010a06

Top of Stack: (sp=0x00000015c43fe790)
0x00000015c43fe790: 00000015c43fe870 00000185d796dff0
0x00000015c43fe7a0: 0000000180006c20 000000006e6246cc
0x00000015c43fe7b0: 00000185d5273818 0000000180006c20
0x00000015c43fe7c0: 00000185d7beb3e0 00000078c43fe7f8
0x00000015c43fe7d0: 00000185d7c52000 0000000000000094
0x00000015c43fe7e0: 00000185d796e3d8 00000185d5273818
0x00000015c43fe7f0: 00000185d7c52000 00000185d5273818
0x00000015c43fe800: 00000185d7c52000 00000185d7c52000
0x00000015c43fe810: 00000185d7c52000 00000015c43fe978
0x00000015c43fe820: 00000185d5273818 0000000000000000
0x00000015c43fe830: 00000185d7c4f070 00007ffae7ac06e1
0x00000015c43fe840: 00000015c43fe950 00000185d7e30000
0x00000015c43fe850: 00000015c43fe950 0000000000000000
0x00000015c43fe860: 0000000000000000 00000185d796e000
0x00000015c43fe870: 00000185bc89843d 0000000180017f18
0x00000015c43fe880: 00000185d5273818 0000000000000005

Instructions: (pc=0x00007ffae7b26d71)
0x00007ffae7b26d51: 0f 0d 0b 4c 8b 43 08 41 8b 45 0c c1 e8 08 0f b7
0x00007ffae7b26d61: d0 48 8b 03 4c 89 44 24 48 89 54 24 3c 48 8b 08
0x00007ffae7b26d71: 41 8b 40 18 41 33 c1 4c 8b 71 18 41 33 c6 41 33
0x00007ffae7b26d81: c0 8b c8 c1 e8 10 0f af c2 0f b7 c9 49 03 c0 48

Register to memory mapping:

RAX=0x000000006ea739f0 is an unknown value
RBX=0x00000185d7beb3e0 is an unknown value
RCX=0x000000006e459ff0 is an unknown value
RDX=0x0000000000000078 is an unknown value
RSP=0x00000015c43fe790 is pointing into the stack for thread: 0x00000185d7c52000
RBP=0x00000185d7e30000 is an unknown value
RSI=0x00000185d7c4f070 is an unknown value
RDI=0x0000000000000000 is an unknown value
R8 =0x0000000000000094 is an unknown value
R9 =0x00000000318ef21f is an unknown value
R10=0x00000185bc8b4c7b is at code_begin+1915 in an Interpreter codelet
method entry point (kind = native) [0x00000185bc8b4500, 0x00000185bc8b57e0] 4832 bytes
R11=0x000000006e67a0a0 is an unknown value
R12=0x0000000000000000 is an unknown value
R13=0x00000185d7c4f060 is an unknown value
R14=0x00000015c43fe978 is pointing into the stack for thread: 0x00000185d7c52000
R15=0x0000000000000000 is an unknown value

Stack: [0x00000015c4300000,0x00000015c4400000], sp=0x00000015c43fe790, free space=1017k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [ntdll.dll+0xa6d71]
C [ntdll.dll+0x406e1]
C [jna8924753396776717146.dll+0x17f18]
C [jna8924753396776717146.dll+0x6c3d]
C 0x00000185bc8b4ca7

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j com.sun.jna.Native.free(J)V+0
j jnademo.DllCallbackImpl2.apply(IJI)V+5
v ~StubRoutines::call_stub
j sun.reflect.NativeMethodAccessorImpl.invoke0(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+0
j sun.reflect.NativeMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+100
j sun.reflect.DelegatingMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+6
j java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+56
j com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback([Ljava/lang/Object;)Ljava/lang/Object;+127
j com.sun.jna.CallbackReference$DefaultCallbackProxy.callback([Ljava/lang/Object;)Ljava/lang/Object;+2
v ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
=>0x00000185d7c52000 JavaThread "playerer_cb2" daemon [_thread_in_native, id=11832, stack(0x00000015c4300000,0x00000015c4400000)]
0x00000185d79fa800 JavaThread "Service Thread" daemon [_thread_blocked, id=17628, stack(0x00000015c4100000,0x00000015c4200000)]
0x00000185d797e800 JavaThread "C1 CompilerThread3" daemon [_thread_blocked, id=16224, stack(0x00000015c4000000,0x00000015c4100000)]
0x00000185d640e800 JavaThread "C2 CompilerThread2" daemon [_thread_blocked, id=4856, stack(0x00000015c3f00000,0x00000015c4000000)]
0x00000185d640d800 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=8904, stack(0x00000015c3e00000,0x00000015c3f00000)]
0x00000185d796d000 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=17648, stack(0x00000015c3d00000,0x00000015c3e00000)]
0x00000185d7969800 JavaThread "JDWP Command Reader" daemon [_thread_in_native, id=13160, stack(0x00000015c3c00000,0x00000015c3d00000)]
0x00000185d640c000 JavaThread "JDWP Event Helper Thread" daemon [_thread_blocked, id=13968, stack(0x00000015c3b00000,0x00000015c3c00000)]
0x00000185d6405800 JavaThread "JDWP Transport Listener: dt_socket" daemon [_thread_blocked, id=7476, stack(0x00000015c3a00000,0x00000015c3b00000)]
0x00000185d6395000 JavaThread "Attach Listener" daemon [_thread_blocked, id=15048, stack(0x00000015c3900000,0x00000015c3a00000)]
0x00000185d6380800 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=1940, stack(0x00000015c3800000,0x00000015c3900000)]
0x00000185d6367800 JavaThread "Finalizer" daemon [_thread_blocked, id=2896, stack(0x00000015c3700000,0x00000015c3800000)]
0x00000185d6338000 JavaThread "Reference Handler" daemon [_thread_blocked, id=17872, stack(0x00000015c3600000,0x00000015c3700000)]
0x00000185baeb9000 JavaThread "main" [_thread_in_native, id=12780, stack(0x00000015c2c00000,0x00000015c2d00000)]

Other Threads:
0x00000185d6330800 VMThread [stack: 0x00000015c3500000,0x00000015c3600000] [id=4716]
0x00000185d7a20000 WatcherThread [stack: 0x00000015c4200000,0x00000015c4300000] [id=18420]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

heap address: 0x00000006c5e00000, size: 4002 MB, Compressed Oops mode: Zero based, Oop shift amount: 3
Narrow klass base: 0x0000000000000000, Narrow klass shift: 3
Compressed class space size: 1073741824 Address: 0x00000007c0000000

Heap:
PSYoungGen total 75264K, used 9032K [0x000000076ca00000, 0x0000000771e00000, 0x00000007c0000000)
eden space 64512K, 14% used [0x000000076ca00000,0x000000076d2d21e0,0x0000000770900000)
from space 10752K, 0% used [0x0000000771380000,0x0000000771380000,0x0000000771e00000)
to space 10752K, 0% used [0x0000000770900000,0x0000000770900000,0x0000000771380000)
ParOldGen total 172032K, used 0K [0x00000006c5e00000, 0x00000006d0600000, 0x000000076ca00000)
object space 172032K, 0% used [0x00000006c5e00000,0x00000006c5e00000,0x00000006d0600000)
Metaspace used 4390K, capacity 5016K, committed 5248K, reserved 1056768K
class space used 492K, capacity 528K, committed 640K, reserved 1048576K

Card table byte_map: [0x00000185cbc50000,0x00000185cc430000] byte_map_base: 0x00000185c8621000

Marking Bits: (ParMarkBitMap*) 0x000000006ec37fe0
Begin Bits: [0x00000185cc970000, 0x00000185d07f8000)
End Bits: [0x00000185d07f8000, 0x00000185d4680000)

Polling page: 0x00000185bae70000

CodeCache: size=245760Kb used=1560Kb max_used=1560Kb free=244199Kb
bounds [0x00000185bc890000, 0x00000185bcb00000, 0x00000185cb890000]
total_blobs=499 nmethods=225 adapters=195
compilation: enabled

Compilation events (10 events):
Event: 0.497 Thread 0x00000185d797e800 220 1 java.security.CodeSource::getLocation (5 bytes)
Event: 0.497 Thread 0x00000185d797e800 nmethod 220 0x00000185bca12350 code [0x00000185bca124a0, 0x00000185bca125b8]
Event: 0.498 Thread 0x00000185d797e800 222 3 java.net.URL::isBuiltinStreamHandler (29 bytes)
Event: 0.498 Thread 0x00000185d797e800 nmethod 222 0x00000185bca140d0 code [0x00000185bca14300, 0x00000185bca15320]
Event: 0.498 Thread 0x00000185d797e800 223 3 java.net.URL::isBuiltinStreamHandler (7 bytes)
Event: 0.498 Thread 0x00000185d797e800 nmethod 223 0x00000185bca15b50 code [0x00000185bca15cc0, 0x00000185bca15fd0]
Event: 0.498 Thread 0x00000185d797e800 224 3 java.util.WeakHashMap::maskNull (12 bytes)
Event: 0.499 Thread 0x00000185d797e800 nmethod 224 0x00000185bca160d0 code [0x00000185bca16220, 0x00000185bca163b8]
Event: 0.499 Thread 0x00000185d797e800 225 3 java.lang.String::indexOf (7 bytes)
Event: 0.499 Thread 0x00000185d797e800 nmethod 225 0x00000185bca16450 code [0x00000185bca165c0, 0x00000185bca16870]

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Classes redefined (0 events):
No events

Internal exceptions (9 events):
Event: 0.028 Thread 0x00000185baeb9000 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x000000076ca07cc0) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hot
Event: 0.028 Thread 0x00000185baeb9000 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x000000076ca07fa8) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hotspot\src\share\vm
Event: 0.135 Thread 0x00000185baeb9000 Exception <a 'java/security/PrivilegedActionException'> (0x000000076cb30118) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hotspot\src\share\vm\prims\jvm.cpp, line 1523]
Event: 0.135 Thread 0x00000185baeb9000 Exception <a 'java/security/PrivilegedActionException'> (0x000000076cb30328) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hotspot\src\share\vm\prims\jvm.cpp, line 1523]
Event: 0.135 Thread 0x00000185baeb9000 Exception <a 'java/security/PrivilegedActionException'> (0x000000076cb33f98) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hotspot\src\share\vm\prims\jvm.cpp, line 1523]
Event: 0.135 Thread 0x00000185baeb9000 Exception <a 'java/security/PrivilegedActionException'> (0x000000076cb341a8) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hotspot\src\share\vm\prims\jvm.cpp, line 1523]
Event: 0.475 Thread 0x00000185baeb9000 Exception <a 'java/lang/UnsatisfiedLinkError': 鎵句笉鍒版寚瀹氱殑妯″潡銆?

(0x000000076cf85fb8) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hotspot\src\share\vm\prims\jni.cpp, line 737]
Event: 0.478 Thread 0x00000185baeb9000 Exception <a 'java/lang/UnsatisfiedLinkError': 鎵句笉鍒版寚瀹氱殑妯″潡銆?
(0x000000076cf862b8) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hotspot\src\share\vm\prims\jni.cpp, line 737]
Event: 0.480 Thread 0x00000185baeb9000 Exception <a 'java/lang/UnsatisfiedLinkError': 鎵句笉鍒版寚瀹氱殑妯″潡銆?
(0x000000076cf86640) thrown at [C:\jenkins\workspace\8-2-build-windows-amd64-cygwin\jdk8u301\1513\hotspot\src\share\vm\prims\jni.cpp, line 737]

Events (10 events):
Event: 4.181 Executing VM operation: GetCurrentLocation
Event: 4.181 Executing VM operation: GetCurrentLocation done
Event: 4.181 Executing VM operation: ChangeSingleStep
Event: 4.181 Executing VM operation: ChangeSingleStep done
Event: 4.182 Executing VM operation: ChangeSingleStep
Event: 4.182 Executing VM operation: ChangeSingleStep done
Event: 4.182 Executing VM operation: GetCurrentLocation
Event: 4.182 Executing VM operation: GetCurrentLocation done
Event: 4.182 Executing VM operation: ChangeSingleStep
Event: 4.182 Executing VM operation: ChangeSingleStep done

Dynamic libraries:
0x00007ff60e820000 - 0x00007ff60e867000 D:\Program Files\Java\jre1.8.0_301\bin\javaw.exe
0x00007ffae7a80000 - 0x00007ffae7c70000 C:\Windows\SYSTEM32\ntdll.dll
0x00007ffae5d10000 - 0x00007ffae5dc2000 C:\Windows\System32\KERNEL32.DLL
0x00007ffae4d70000 - 0x00007ffae5016000 C:\Windows\System32\KERNELBASE.dll
0x00007ffae64e0000 - 0x00007ffae6583000 C:\Windows\System32\ADVAPI32.dll
0x00007ffae5b20000 - 0x00007ffae5bbe000 C:\Windows\System32\msvcrt.dll
0x00007ffae6590000 - 0x00007ffae6627000 C:\Windows\System32\sechost.dll
0x00007ffae7840000 - 0x00007ffae795f000 C:\Windows\System32\RPCRT4.dll
0x00007ffae68b0000 - 0x00007ffae6a44000 C:\Windows\System32\USER32.dll
0x00007ffae5320000 - 0x00007ffae5341000 C:\Windows\System32\win32u.dll
0x00007ffae6630000 - 0x00007ffae6656000 C:\Windows\System32\GDI32.dll
0x00007ffae5020000 - 0x00007ffae51b8000 C:\Windows\System32\gdi32full.dll
0x00007ffae4a00000 - 0x00007ffae4a9e000 C:\Windows\System32\msvcp_win.dll
0x00007ffae4b50000 - 0x00007ffae4c4a000 C:\Windows\System32\ucrtbase.dll
0x00007ffacba60000 - 0x00007ffacbce4000 C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.18362.1734_none_9e7c63e3e45fdc87\COMCTL32.dll
0x00007ffae6170000 - 0x00007ffae64a6000 C:\Windows\System32\combase.dll
0x00007ffae4ce0000 - 0x00007ffae4d64000 C:\Windows\System32\bcryptPrimitives.dll
0x00007ffae64b0000 - 0x00007ffae64de000 C:\Windows\System32\IMM32.DLL
0x00007ffadf640000 - 0x00007ffadf655000 D:\Program Files\Java\jre1.8.0_301\bin\vcruntime140.dll
0x00007ffac2df0000 - 0x00007ffac2e8b000 D:\Program Files\Java\jre1.8.0_301\bin\msvcp140.dll
0x000000006e450000 - 0x000000006ecb0000 D:\Program Files\Java\jre1.8.0_301\bin\server\jvm.dll
0x00007ffae7640000 - 0x00007ffae7648000 C:\Windows\System32\PSAPI.DLL
0x00007ffae24e0000 - 0x00007ffae2504000 C:\Windows\SYSTEM32\WINMM.dll
0x00007ffacc6f0000 - 0x00007ffacc6f9000 C:\Windows\SYSTEM32\WSOCK32.dll
0x00007ffaddb60000 - 0x00007ffaddb6a000 C:\Windows\SYSTEM32\VERSION.dll
0x00007ffae6100000 - 0x00007ffae616f000 C:\Windows\System32\WS2_32.dll
0x00007ffae24b0000 - 0x00007ffae24dd000 C:\Windows\SYSTEM32\WINMMBASE.dll
0x00007ffae5ad0000 - 0x00007ffae5b1a000 C:\Windows\System32\cfgmgr32.dll
0x00007ffae4950000 - 0x00007ffae4961000 C:\Windows\System32\kernel.appcore.dll
0x00007ffadf5c0000 - 0x00007ffadf5d0000 D:\Program Files\Java\jre1.8.0_301\bin\verify.dll
0x00007ffad4a80000 - 0x00007ffad4aab000 D:\Program Files\Java\jre1.8.0_301\bin\java.dll
0x00007ffacc790000 - 0x00007ffacc7c6000 D:\Program Files\Java\jre1.8.0_301\bin\jdwp.dll
0x00007ffadfa30000 - 0x00007ffadfa39000 D:\Program Files\Java\jre1.8.0_301\bin\npt.dll
0x00007ffadf520000 - 0x00007ffadf538000 D:\Program Files\Java\jre1.8.0_301\bin\zip.dll
0x00007ffae6f50000 - 0x00007ffae7637000 C:\Windows\System32\SHELL32.dll
0x00007ffae7790000 - 0x00007ffae7839000 C:\Windows\System32\shcore.dll
0x00007ffae5350000 - 0x00007ffae5acb000 C:\Windows\System32\windows.storage.dll
0x00007ffae49c0000 - 0x00007ffae49de000 C:\Windows\System32\profapi.dll
0x00007ffae4970000 - 0x00007ffae49ba000 C:\Windows\System32\powrprof.dll
0x00007ffae4920000 - 0x00007ffae4930000 C:\Windows\System32\UMPDC.dll
0x00007ffae5bd0000 - 0x00007ffae5c22000 C:\Windows\System32\shlwapi.dll
0x00007ffae49e0000 - 0x00007ffae49f7000 C:\Windows\System32\cryptsp.dll
0x00007ffadf5d0000 - 0x00007ffadf5da000 D:\Program Files\Java\jre1.8.0_301\bin\dt_socket.dll
0x00007ffadf590000 - 0x00007ffadf5a6000 C:\Windows\system32\napinsp.dll
0x00007ffadf570000 - 0x00007ffadf58a000 C:\Windows\system32\pnrpnsp.dll
0x00007ffae4170000 - 0x00007ffae41d7000 C:\Windows\System32\mswsock.dll
0x00007ffae3ec0000 - 0x00007ffae3f8a000 C:\Windows\SYSTEM32\DNSAPI.dll
0x00007ffae5bc0000 - 0x00007ffae5bc8000 C:\Windows\System32\NSI.dll
0x00007ffae3e80000 - 0x00007ffae3eba000 C:\Windows\SYSTEM32\IPHLPAPI.DLL
0x00007ffadfab0000 - 0x00007ffadfabe000 C:\Windows\System32\winrnr.dll
0x00007ffade360000 - 0x00007ffade37c000 C:\Windows\system32\NLAapi.dll
0x00007ffab8980000 - 0x00007ffab8995000 C:\Windows\system32\wshbth.dll
0x00007ffadcb10000 - 0x00007ffadcb87000 C:\Windows\System32\fwpuclnt.dll
0x00007ffae4c50000 - 0x00007ffae4c76000 C:\Windows\System32\bcrypt.dll
0x00007ffad6210000 - 0x00007ffad621a000 C:\Windows\System32\rasadhlp.dll
0x00007ffae3cf0000 - 0x00007ffae3d23000 C:\Windows\system32\rsaenh.dll
0x00007ffae4840000 - 0x00007ffae4865000 C:\Windows\SYSTEM32\USERENV.dll
0x00007ffae4340000 - 0x00007ffae434c000 C:\Windows\SYSTEM32\CRYPTBASE.dll
0x00007ffade070000 - 0x00007ffade08c000 D:\Program Files\Java\jre1.8.0_301\bin\net.dll
0x00007ffadcd50000 - 0x00007ffadcd66000 C:\Windows\SYSTEM32\dhcpcsvc6.DLL
0x00007ffadcd30000 - 0x00007ffadcd4c000 C:\Windows\SYSTEM32\dhcpcsvc.DLL
0x00007ffadcc70000 - 0x00007ffadcc83000 D:\Program Files\Java\jre1.8.0_301\bin\nio.dll
0x0000000180000000 - 0x0000000180038000 C:\Users\Administrator\AppData\Local\Temp\jna-146731693\jna8924753396776717146.dll
0x00007ffade0b0000 - 0x00007ffade0ba000 \?\D:\javacb\jnademo\bin\usbkey.dll
0x00007ffae3230000 - 0x00007ffae3424000 C:\Windows\SYSTEM32\dbghelp.dll

VM Arguments:
jvm_args: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:3321 -Dfile.encoding=GBK
java_command: jnademo.TestDllCallback
java_class_path (initial): D:\javacb\jnademo\bin;D:\javacb\jnademo\lib\jnaerator-0.13-SNAPSHOT-shaded.jar;D:\javacb\jnademo\lib\jnaerator-runtime-0.13-SNAPSHOT.jar;D:\javacb\jna\usbkey.jar
Launcher Type: SUN_STANDARD

Environment Variables:
PATH=D:/Program Files/Java/jre1.8.0_301/bin/server;D:/Program Files/Java/jre1.8.0_301/bin;D:/Program Files/Java/jre1.8.0_301/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files\ImageMagick-7.0.11-Q16-HDRI;E:\tools\Python39\Scripts;E:\tools\Python39;C:\Program Files (x86)\VMware\VMware Workstation\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit;C:\Program Files\Git\cmd;C:\Program Files\Microsoft VS Code\bin;C:\Program Files\CMake\bin;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;D:\my\components\ffmpeg-4.3.1-2020-10-01-full_build-shared\bin;E:\tools\jdk-11.0.3\bin;D:\android\android-sdk-windows\platform-tools;D:\android\android-ndk-r17c;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn;C:\Program Files\dotnet;E:\tools\nasm-2.13.03;C:\Program Files\OpenSSL-Win64\bin;C:\Program Files (x86)\Paragon Software\ExtFS for Windows;D:\Qt\5.15.2\msvc2019\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Local\Programs\EmEditor;C:\Users\Administrator.dotnet\tools;;D:\eclipse;
USERNAME=Administrator
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 142 Stepping 10, GenuineIntel

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

OS: Windows 10.0 , 64 bit Build 18362 (10.0.18362.1714)

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

Memory: 4k page, physical 16390048k(9241360k free), swap 23992224k(13557020k free)

vm_info: Java HotSpot(TM) 64-Bit Server VM (25.301-b09) for windows-amd64 JRE (1.8.0_301-b09), built on Jun 9 2021 06:46:21 by "java_re" with MS VC++ 15.9 (VS2017)

Daniel Widdis

unread,
Aug 16, 2021, 1:39:44 AM8/16/21
to jna-...@googlegroups.com

I’m a bit confused where you are doing the memory allocation.   You’ve only shown C code but indicated you’re freeing on the Java side.

 

Does your callback allocate the memory natively and return it to Java?

 

Do you allocate a structure in Java and pass its pointer to native?

 

Please post more complete actual code/context.

--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jna-users/5ad59cda-ea4a-4189-a7be-381a0bb5762fn%40googlegroups.com.

Mark Hong

unread,
Aug 16, 2021, 1:48:23 AM8/16/21
to Java Native Access
Yes, I allocate the memory in the c++ native function and then callback to java.
In Java, I continue to post the memory to the main thread(UI thread) and after handle it, then release the memory in Java.
the memory allocation code (in C++) is as following:
repFileInfo = (FFRepFileInfo*)malloc(sizeof(FFRepFileInfo));
repFileInfo->duration = (long long)repFileInfo;
repFileInfo->audioCodec = 1;
repFileInfo->channelCount = 2;
repFileInfo->sampleRate = 44100;
repFileInfo->videoCodec = 3;
repFileInfo->videoFrameRate = 25;
repFileInfo->videoWidth = 1920;
repFileInfo->videoHeight = 1080;

(*g_cb2)(2209, (long long)repFileInfo, g_userData);
 
it's a pointer of structure FFRepFileInfo, which is pass to the callback using (long long)reFileInfo

thanks!

Daniel Widdis

unread,
Aug 16, 2021, 3:00:02 AM8/16/21
to jna-...@googlegroups.com

And can you show your java method and the sequence of calling the native method and your use of Native.free()?

Mark Hong

unread,
Aug 16, 2021, 3:05:56 AM8/16/21
to Java Native Access
@Override
public void apply(int msg, long param, int userData) {
Pointer ptr = new Pointer(param);
// 将指针转换为结构,直接访问
tagFFRepFileInfo u = (tagFFRepFileInfo) Structure.newInstance(tagFFRepFileInfo.class, ptr);
u.read();
System.out.println("duration:" + u.duration);
// free the memory
Native.free(param); // <-- here crashes

Matthias Bläsing

unread,
Aug 16, 2021, 1:47:51 PM8/16/21
to jna-...@googlegroups.com
Hi,

youre messages are difficult to read. It would be helpful, if you could
provide:

* a runnable sample or at least the full source code that reproduces
the problem
* the C header files for the stucture and the callback

You write that the C signature of the call back is:

DLL_API typedef void(mycallback)(void ptr, int userData);

and the Java signature is:

public void apply(int msg, long param, int userData);


I don't see the connection between the two (different data types and
count). I would bind the callback as:

public void apply(Pointer ptr, int userData);

Greetings

Matthias


Am Montag, dem 16.08.2021 um 00:05 -0700 schrieb 'Mark Hong' via Java
Native Access:
Reply all
Reply to author
Forward
0 new messages