Why calling GetDefaultAudioRenderId from a c++ dll in Java with JNA is resulting in a "java.lang.Error: Invalid memory access"?

-1

I'm trying to make a sound player for Java using the Superpowered audio API. I succeed to compile and produce a simple .dll file that use the Superpowerd Api to play a sound. I load it with JNA from my Java code. When I try to launch the player it fails at this line, on the c++ part :

Platform::String^ outputDeviceId = Windows::Media::Devices::MediaDevice::GetDefaultAudioRenderId(Windows::Media::Devices::AudioDeviceRole::Default);

It seems that calling GetDefaultAudioRenderId crash the app, then I get this exception :

Exception in thread "main" java.lang.Error: Invalid memory access
    at com.nccoding.keoplayer.audio.jna.MultiTrackAudioPlayer.play(Native Method)
    at com.nccoding.keoplayer.audio.jna.MainTest.main(MainTest.java:9)

Do you know why ? is there something special to set on the c++ dll project to let the dll access to these king of windows systems calls ?

For information, I'm using Visual Studio 2019 to create the .dll file.

Edit : I've tried to load it with JNI this time (instead of JNA) and i get a more detailed error :

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_UNCAUGHT_CXX_EXCEPTION (0xe06d7363) at pc=0x76c43442, pid=2900, tid=0x000038e4
#
# JRE version: Java(TM) SE Runtime Environment (8.0_161-b12) (build 1.8.0_161-b12)
# Java VM: Java HotSpot(TM) Client VM (25.161-b12 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [KERNELBASE.dll+0x113442]
#
# 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 (0x02890800):  JavaThread "main" [_thread_in_native, id=14564, stack(0x00fc0000,0x01010000)]

siginfo: ExceptionCode=0xe06d7363, ExceptionInformation=0x19930520 0x0100f988 0x55fbe2f0 

Registers:
EAX=0x0100f8c8, EBX=0x0100f988, ECX=0x00000003, EDX=0x00000000
ESP=0x0100f8c8, EBP=0x0100f920, ESI=0x55fb0090, EDI=0x55fbe2f0
EIP=0x76c43442, EFLAGS=0x00000212

Top of Stack: (sp=0x0100f8c8)
0x0100f8c8:   e06d7363 00000001 00000000 76c43442
0x0100f8d8:   00000003 19930520 0100f988 55fbe2f0
0x0100f8e8:   00000000 168e89f0 75fd3a60 168e89f4
0x0100f8f8:   168e84b4 168e8a34 00000000 0100f940
0x0100f908:   55fbe2f0 55fb0104 76111260 0100f940
0x0100f918:   55fafffd 85717b12 0100f964 56074515
0x0100f928:   e06d7363 00000001 00000003 0100f958
0x0100f938:   02890800 00ef1ec8 14e30428 e06d7363 

Instructions: (pc=0x76c43442)
0x76c43422:   89 44 24 10 c1 e0 02 50 51 8d 44 24 1c 50 e8 df
0x76c43432:   50 02 00 83 c4 0c 8d 04 24 50 ff 15 04 84 cf 76
0x76c43442:   8b 4c 24 54 33 cc e8 d3 bc 01 00 8b e5 5d c2 10
0x76c43452:   00 83 64 24 10 00 eb de 6a 0f 58 eb c3 cc cc cc 


Register to memory mapping:

EAX=0x0100f8c8 is pointing into the stack for thread: 0x02890800
EBX=0x0100f988 is pointing into the stack for thread: 0x02890800
ECX=0x00000003 is an unknown value
EDX=0x00000000 is an unknown value
ESP=0x0100f8c8 is pointing into the stack for thread: 0x02890800
EBP=0x0100f920 is pointing into the stack for thread: 0x02890800
ESI=0x55fb0090 is an unknown value
EDI=0x55fbe2f0 is an unknown value


Stack: [0x00fc0000,0x01010000],  sp=0x0100f8c8,  free space=318k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [KERNELBASE.dll+0x113442]
C  [VCRUNTIME140_APP.dll+0x4515]
C  [vccorlib140_app.DLL+0x211c3]
C  [MultiTrackAudioPlayer.dll+0x1307]  __abi_WinRTraiseException+0xb7
C  [MultiTrackAudioPlayer.dll+0x11a0]  Windows::Media::Devices::MediaDevice::GetDefaultAudioRenderId+0xd0
C  [MultiTrackAudioPlayer.dll+0x15a5]  Java_com_nccoding_keoplayer_audio_jni_MultiTrackAudioPlayerJNI_play+0xc5
j  com.nccoding.keoplayer.audio.jni.MultiTrackAudioPlayerJNI.main([Ljava/lang/String;)V+38
v  ~StubRoutines::call_stub
V  [jvm.dll+0x15b6d5]
V  [jvm.dll+0x22229e]
V  [jvm.dll+0x15b76e]
V  [jvm.dll+0xdd0c7]
V  [jvm.dll+0xe565f]
C  [javaw.exe+0x22ab]
C  [javaw.exe+0xaf8f]
C  [javaw.exe+0xb019]
C  [KERNEL32.DLL+0x16359]
C  [ntdll.dll+0x67a94]
C  [ntdll.dll+0x67a64]
C  0x00000000

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.nccoding.keoplayer.audio.jni.MultiTrackAudioPlayerJNI.play()V+0
j  com.nccoding.keoplayer.audio.jni.MultiTrackAudioPlayerJNI.main([Ljava/lang/String;)V+38
v  ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
  0x0293b800 JavaThread "Service Thread" daemon [_thread_blocked, id=15904, stack(0x15400000,0x15450000)]
  0x02924800 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=15764, stack(0x15370000,0x153c0000)]
  0x02923800 JavaThread "Attach Listener" daemon [_thread_blocked, id=13732, stack(0x152e0000,0x15330000)]
  0x02918c00 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=14652, stack(0x15250000,0x152a0000)]
  0x02917000 JavaThread "Finalizer" daemon [_thread_blocked, id=13900, stack(0x150a0000,0x150f0000)]
  0x028b9000 JavaThread "Reference Handler" daemon [_thread_blocked, id=5024, stack(0x04b80000,0x04bd0000)]
=>0x02890800 JavaThread "main" [_thread_in_native, id=14564, stack(0x00fc0000,0x01010000)]

Other Threads:
  0x028b6000 VMThread [stack: 0x04af0000,0x04b40000] [id=13080]
  0x02959800 WatcherThread [stack: 0x15490000,0x154e0000] [id=10384]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap:
 def new generation   total 4928K, used 883K [0x04c00000, 0x05150000, 0x0a150000)
  eden space 4416K,  20% used [0x04c00000, 0x04cdcfc8, 0x05050000)
  from space 512K,   0% used [0x05050000, 0x05050000, 0x050d0000)
  to   space 512K,   0% used [0x050d0000, 0x050d0000, 0x05150000)
 tenured generation   total 10944K, used 0K [0x0a150000, 0x0ac00000, 0x14c00000)
   the space 10944K,   0% used [0x0a150000, 0x0a150000, 0x0a150200, 0x0ac00000)
 Metaspace       used 47K, capacity 2242K, committed 2368K, reserved 4480K

Card table byte_map: [0x04a20000,0x04ab0000] byte_map_base: 0x049fa000

Polling page: 0x00d20000

CodeCache: size=32768Kb used=728Kb max_used=728Kb free=32039Kb
 bounds [0x02a20000, 0x02ad8000, 0x04a20000]
 total_blobs=166 nmethods=28 adapters=69
 compilation: enabled

Compilation events (10 events):
Event: 0.056 Thread 0x02924800   24   !         sun.net.www.ParseUtil::decode (316 bytes)
Event: 0.057 Thread 0x02924800 nmethod 24 0x02ad2708 code [0x02ad29c0, 0x02ad3864]
Event: 0.057 Thread 0x02924800   25             java.lang.Math::min (11 bytes)
Event: 0.057 Thread 0x02924800 nmethod 25 0x02ad4d08 code [0x02ad4e00, 0x02ad4e70]
Event: 0.057 Thread 0x02924800   26             java.util.HashMap::hash (20 bytes)
Event: 0.057 Thread 0x02924800 nmethod 26 0x02ad4ec8 code [0x02ad4fc0, 0x02ad505c]
Event: 0.057 Thread 0x02924800   27             java.lang.String::indexOf (166 bytes)
Event: 0.057 Thread 0x02924800 nmethod 27 0x02ad54c8 code [0x02ad55e0, 0x02ad5810]
Event: 0.057 Thread 0x02924800   28             java.lang.String::replace (127 bytes)
Event: 0.057 Thread 0x02924800 nmethod 28 0x02ad5b48 code [0x02ad5c60, 0x02ad5ee0]

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Classes redefined (0 events):
No events

Internal exceptions (2 events):
Event: 0.017 Thread 0x02890800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x04c079b8) thrown at [C:\re\workspace\8-2-build-windows-i586-cygwin\jdk8u161\10277\hotspot\src\shar
Event: 0.017 Thread 0x02890800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x04c07c88) thrown at [C:\re\workspace\8-2-build-windows-i586-cygwin\jdk8u161\10277\hotspot\src\share\vm\prims\jni.cpp

Events (10 events):
Event: 0.048 loading class sun/nio/cs/ThreadLocalCoders
Event: 0.049 loading class sun/nio/cs/ThreadLocalCoders done
Event: 0.049 loading class sun/nio/cs/ThreadLocalCoders$1
Event: 0.049 loading class sun/nio/cs/ThreadLocalCoders$Cache
Event: 0.049 loading class sun/nio/cs/ThreadLocalCoders$Cache done
Event: 0.049 loading class sun/nio/cs/ThreadLocalCoders$1 done
Event: 0.049 loading class sun/nio/cs/ThreadLocalCoders$2
Event: 0.049 loading class sun/nio/cs/ThreadLocalCoders$2 done
Event: 0.056 loading class sun/misc/URLClassPath$FileLoader$1
Event: 0.056 loading class sun/misc/URLClassPath$FileLoader$1 done


Dynamic libraries:
0x00060000 - 0x00093000     C:\Program Files (x86)\Java\jdk1.8.0_161\bin\javaw.exe
0x77730000 - 0x778ca000     C:\WINDOWS\SYSTEM32\ntdll.dll
0x772c0000 - 0x773a0000     C:\WINDOWS\System32\KERNEL32.DLL
0x76b30000 - 0x76d2c000     C:\WINDOWS\System32\KERNELBASE.dll
0x77570000 - 0x775e9000     C:\WINDOWS\System32\ADVAPI32.dll
0x771a0000 - 0x7725f000     C:\WINDOWS\System32\msvcrt.dll
0x763d0000 - 0x76446000     C:\WINDOWS\System32\sechost.dll
0x770c0000 - 0x7717b000     C:\WINDOWS\System32\RPCRT4.dll
0x74f00000 - 0x74f20000     C:\WINDOWS\System32\SspiCli.dll
0x74ef0000 - 0x74efa000     C:\WINDOWS\System32\CRYPTBASE.dll
0x768a0000 - 0x768ff000     C:\WINDOWS\System32\bcryptPrimitives.dll
0x76ef0000 - 0x77085000     C:\WINDOWS\System32\USER32.dll
0x75e30000 - 0x75e47000     C:\WINDOWS\System32\win32u.dll
0x76b00000 - 0x76b21000     C:\WINDOWS\System32\GDI32.dll
0x76900000 - 0x76a5a000     C:\WINDOWS\System32\gdi32full.dll
0x751d0000 - 0x7524c000     C:\WINDOWS\System32\msvcp_win.dll
0x74f20000 - 0x7503f000     C:\WINDOWS\System32\ucrtbase.dll
0x6eb90000 - 0x6ed9f000     C:\WINDOWS\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.18362.295_none_2e70e394278c3b98\COMCTL32.dll
0x75fd0000 - 0x76246000     C:\WINDOWS\System32\combase.dll
0x75040000 - 0x75065000     C:\WINDOWS\System32\IMM32.DLL
0x557f0000 - 0x558ad000     C:\Program Files (x86)\Java\jdk1.8.0_161\jre\bin\msvcr100.dll
0x54290000 - 0x54660000     C:\Program Files (x86)\Java\jdk1.8.0_161\jre\bin\client\jvm.dll
0x770b0000 - 0x770b6000     C:\WINDOWS\System32\PSAPI.DLL
0x72290000 - 0x72298000     C:\WINDOWS\SYSTEM32\WSOCK32.dll
0x74930000 - 0x74938000     C:\WINDOWS\SYSTEM32\VERSION.dll
0x73210000 - 0x73234000     C:\WINDOWS\SYSTEM32\WINMM.dll
0x76300000 - 0x7635e000     C:\WINDOWS\System32\WS2_32.dll
0x72380000 - 0x723a3000     C:\WINDOWS\SYSTEM32\winmmbase.dll
0x773b0000 - 0x773eb000     C:\WINDOWS\System32\cfgmgr32.dll
0x5ed60000 - 0x5ed6c000     C:\Program Files (x86)\Java\jdk1.8.0_161\jre\bin\verify.dll
0x560b0000 - 0x560d2000     C:\Program Files (x86)\Java\jdk1.8.0_161\jre\bin\java.dll
0x56090000 - 0x560a4000     C:\Program Files (x86)\Java\jdk1.8.0_161\jre\bin\zip.dll
0x758a0000 - 0x75e15000     C:\WINDOWS\System32\SHELL32.dll
0x775f0000 - 0x77674000     C:\WINDOWS\System32\shcore.dll
0x752d0000 - 0x75892000     C:\WINDOWS\System32\windows.storage.dll
0x752b0000 - 0x752c7000     C:\WINDOWS\System32\profapi.dll
0x76250000 - 0x76293000     C:\WINDOWS\System32\powrprof.dll
0x76e30000 - 0x76e3d000     C:\WINDOWS\System32\UMPDC.dll
0x75180000 - 0x751c4000     C:\WINDOWS\System32\shlwapi.dll
0x773a0000 - 0x773af000     C:\WINDOWS\System32\kernel.appcore.dll
0x763b0000 - 0x763c3000     C:\WINDOWS\System32\cryptsp.dll
0x553d0000 - 0x5547c000     C:\Users\Djos\git\KeoPlayer\KeoPlayer\jnidll\MultiTrackAudioPlayer.dll
0x56070000 - 0x56084000     C:\Users\Djos\git\KeoPlayer\KeoPlayer\VCRUNTIME140_APP.dll
0x55f90000 - 0x55fcf000     C:\Users\Djos\git\KeoPlayer\KeoPlayer\vccorlib140_app.DLL
0x77680000 - 0x77712000     C:\WINDOWS\System32\OLEAUT32.dll
0x55780000 - 0x557e8000     C:\Users\Djos\git\KeoPlayer\KeoPlayer\MSVCP140_APP.dll
0x720e0000 - 0x72132000     C:\WINDOWS\system32\mswsock.dll
0x741a0000 - 0x74231000     C:\WINDOWS\SYSTEM32\DNSAPI.dll
0x76af0000 - 0x76af7000     C:\WINDOWS\System32\NSI.dll
0x74d90000 - 0x74dc2000     C:\WINDOWS\SYSTEM32\IPHLPAPI.DLL
0x77180000 - 0x77199000     C:\WINDOWS\System32\bcrypt.dll
0x6ff20000 - 0x6ff28000     C:\Windows\System32\rasadhlp.dll
0x6e9e0000 - 0x6eb6f000     C:\WINDOWS\SYSTEM32\dbghelp.dll

VM Arguments:
jvm_args: -Djava.library.path=C:\Users\Djos\git\KeoPlayer\KeoPlayer\jnidll -Dfile.encoding=Cp1252 
java_command: com.nccoding.keoplayer.audio.jni.MultiTrackAudioPlayerJNI
java_class_path (initial): C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\rt.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;C:\Users\Djos\git\KeoPlayer\KeoPlayer\target\classes;C:\Users\Djos\git\KeoPlayerCommonLibrary\KeoPlayerCommonLibrary\target\classes;C:\Users\Djos\.m2\repository\org\json\json\20180130\json-20180130.jar;C:\Users\Djos\git\KeoPushClient\KeoPushClient\target\classes;C:\Users\Djos\.m2\repository\javax\websocket\javax.websocket-api\1.1\javax.websocket-api-1.1.jar;C:\Users\Djos\.m2\repository\org\glassfish\tyrus\tyrus-server\1.12\tyrus-server-1.12.jar;C:\Users\Djos\.m2\repository\org\glassfish\tyrus\tyrus-client\1.12\tyrus-client-1.12.jar;C:\Users\Djos\.m2\repository\org\glassfish\tyrus\tyrus-core\1.12\tyrus-core-1.12.jar;C:\Users\Djos\.m2\repository\org\glassfish\tyrus\tyrus-spi\1.12\tyrus-spi-1.12.jar;C:\Users\Djos\.m2\repository\org\glassfish\tyrus\tyrus-container-grizzly-client\1.12\tyrus-container-grizzly-client
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_92
PATH=C:\Windows\System32
USERNAME=Djos
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 18362 (10.0.18362.86)

CPU:total 8 (initial active 8) (4 cores per cpu, 2 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, ht, tsc, tscinvbit, bmi1, bmi2, adx

Memory: 4k page, physical 16715984k(8575400k free), swap 19730640k(7979876k free)

vm_info: Java HotSpot(TM) Client VM (25.161-b12) for windows-x86 JRE (1.8.0_161-b12), built on Dec 19 2017 17:09:29 by "java_re" with MS VC++ 10.0 (VS2010)

time: Mon Sep 02 17:51:00 2019
elapsed time: 0 seconds (0d 0h 0m 0s)


java
c++
windows
dll
jna
asked on Stack Overflow Aug 30, 2019 by NC Coding • edited Sep 2, 2019 by NC Coding

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0