JNA指针无效的内存访问导致EXCEPTION_ACCESS_VIOLATION?

时间:2011-03-02 04:01:41

标签: java c++ pointers java-native-interface jna

我正在尝试使用JNA从Java运行以下C ++方法,但我得到了一个EXCEPTION_ACCESS_VIOLATION。

typedef struct sp_session sp_session; ///< Representation of a session
/**
* The connection state of the specified session.
*
* @param[in]   session    Your session object
*
* @return                 The connection state - see the sp_connectionstate enum for possible values
*/
SP_LIBEXPORT(sp_connectionstate) sp_session_connectionstate(sp_session *session);

这是我的Java代码:

主要课程

public class Test{
    static{
        System.loadLibrary("libspotify");
    }

    public static void main(String[] args){
        JLibspotify lib = (JLibspotify)Native.loadLibrary("libspotify", JLibspotify.class);

        sp_session mySession = new sp_session();

        lib.sessionConnectionstate(mySession); // CRASH_HERE
    }
}

文库

public interface JLibspotify extends StdCallLibrary{
    int sessionConnectionstate(sp_session session);
}

sp_session对象我将此声明为PointerType,因为我没有任何关于它的信息

public class sp_session extends PointerType{

    public sp_session(Pointer address) {
        super(address);
    }
    public sp_session() {
        super();
    }
}

这是我的崩溃报告:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x1008dbf5, pid=4984, tid=6000
#
# JRE version: 7.0-b129
# Java VM: Java HotSpot(TM) Client VM (21.0-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [libspotify.dll+0x8dbf5]
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/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 (0x00a49400):  JavaThread "main" [_thread_in_native, id=6000, stack(0x00ba0000,0x00bf0000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000

Registers:
EAX=0x00000000, EBX=0x334e8098, ECX=0x00000001, EDX=0x00000004
ESP=0x00bef7bc, EBP=0x00bef7c8, ESI=0x334e8098, EDI=0x00a49400
EIP=0x1008dbf5, EFLAGS=0x00010212

Top of Stack: (sp=0x00bef7bc)
0x00bef7bc:   00000001 0470dac7 00000000 00bef828
0x00bef7cc:   047091b9 04708eb0 00bef810 00000004
0x00bef7dc:   00000001 00befaa0 1008dbf0 650dcd94
0x00bef7ec:   00a49400 00a49400 334e8098 650dce6a
0x00bef7fc:   00a49400 00000400 0000000a 00bef828
0x00bef80c:   00000002 00befa30 00befaa0 00bef850
0x00bef81c:   00bef874 00000000 00000004 00befa78
0x00bef82c:   04702161 00befa30 1008dbf0 00befaa0 

Instructions: (pc=0x1008dbf5)
0x1008dbd5:   0b 8b 01 8b 50 0c ff d2 5e 5b 59 c2 04 00 cc cc
0x1008dbe5:   cc cc cc cc cc cc cc cc cc cc cc 51 8b 44 24 08
0x1008dbf5:   8b 08 8b 11 8b 42 10 c7 04 24 00 00 00 00 ff d0
0x1008dc05:   59 c2 04 00 cc cc cc cc cc cc cc 51 53 55 8b 6c 


Register to memory mapping:

EAX=0x00000000 is an unknown value
EBX=0x334e8098 is an oop
{method} 
 - klass: {other class}
ECX=0x00000001 is an unknown value
EDX=0x00000004 is an unknown value
ESP=0x00bef7bc is pointing into the stack for thread: 0x00a49400
EBP=0x00bef7c8 is pointing into the stack for thread: 0x00a49400
ESI=0x334e8098 is an oop
{method} 
 - klass: {other class}
EDI=0x00a49400 is a thread


Stack: [0x00ba0000,0x00bf0000],  sp=0x00bef7bc,  free space=317k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [libspotify.dll+0x8dbf5]  sp_session_connectionstate+0x5
C  [jna8472051172185323617.tmp+0x91b9]  JNI_OnUnload+0xda6
C  [jna8472051172185323617.tmp+0x2161]
C  [jna8472051172185323617.tmp+0x2849]  Java_com_sun_jna_Function_invokeInt+0x35
j  com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;+309
j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+194
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+344
j  $Proxy0.sessionConnectionstate(Lcom/nbarraille/jspotify/model/sp_session;)I+16
j  com.nbarraille.jspotify.main.Test.main([Ljava/lang/String;)V+110
v  ~StubRoutines::call_stub
V  [jvm.dll+0x115f6d]
V  [jvm.dll+0x1b788e]
V  [jvm.dll+0x115fed]
V  [jvm.dll+0xa2507]
V  [jvm.dll+0xac867]
C  [javaw.exe+0x209e]
C  [javaw.exe+0xa23b]
C  [javaw.exe+0xa2c5]
C  [kernel32.dll+0x51194]  BaseThreadInitThunk+0x12
C  [ntdll.dll+0x5b429]  RtlInitializeExceptionChain+0x63
C  [ntdll.dll+0x5b3fc]  RtlInitializeExceptionChain+0x36

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;+309
j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+194
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+344
j  $Proxy0.sessionConnectionstate(Lcom/nbarraille/jspotify/model/sp_session;)I+16
j  com.nbarraille.jspotify.main.Test.main([Ljava/lang/String;)V+110
v  ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
  0x009ffc00 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=680, stack(0x01120000,0x01170000)]
  0x009fac00 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=5032, stack(0x00f00000,0x00f50000)]
  0x009f9800 JavaThread "Attach Listener" daemon [_thread_blocked, id=6360, stack(0x01090000,0x010e0000)]
  0x009f6400 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=652, stack(0x01010000,0x01060000)]
  0x009ed000 JavaThread "Finalizer" daemon [_thread_blocked, id=1148, stack(0x00d30000,0x00d80000)]
  0x009e7800 JavaThread "Reference Handler" daemon [_thread_blocked, id=6172, stack(0x00f50000,0x00fa0000)]
=>0x00a49400 JavaThread "main" [_thread_in_native, id=6000, stack(0x00ba0000,0x00bf0000)]

Other Threads:
  0x009e6400 VMThread [stack: 0x00d90000,0x00de0000] [id=4112]
  0x00a13000 WatcherThread [stack: 0x03e70000,0x03ec0000] [id=4168]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 4928K, used 1525K [0x23450000, 0x239a0000, 0x289a0000)
  eden space 4416K,  34% used [0x23450000, 0x235cd6c8, 0x238a0000)
  from space 512K,   0% used [0x238a0000, 0x238a0000, 0x23920000)
  to   space 512K,   0% used [0x23920000, 0x23920000, 0x239a0000)
 tenured generation   total 10944K, used 0K [0x289a0000, 0x29450000, 0x33450000)
   the space 10944K,   0% used [0x289a0000, 0x289a0000, 0x289a0200, 0x29450000)
 compacting perm gen  total 12288K, used 638K [0x33450000, 0x34050000, 0x37450000)
   the space 12288K,   5% used [0x33450000, 0x334efb70, 0x334efc00, 0x34050000)
    ro space 10240K,  43% used [0x37450000, 0x3789ce40, 0x3789d000, 0x37e50000)
    rw space 12288K,  53% used [0x37e50000, 0x384c2710, 0x384c2800, 0x38a50000)

Code Cache  [0x01e50000, 0x01ed8000, 0x03e50000)
 total_blobs=187 nmethods=39 adapters=85 free_code_cache=33013568 largest_free_block=0

Dynamic libraries:
0x01220000 - 0x01250000     C:\Program Files\Java\jdk1.7.0\bin\javaw.exe
0x76dc0000 - 0x76efd000     C:\Windows\SYSTEM32\ntdll.dll
0x76c10000 - 0x76ce4000     C:\Windows\system32\kernel32.dll
0x75110000 - 0x7515a000     C:\Windows\system32\KERNELBASE.dll
0x60000000 - 0x60041000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\midas32.dll
0x61000000 - 0x61028000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_base.m32
0x67000000 - 0x67048000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_nt.m32
0x64000000 - 0x64021000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_registry.m32
0x62000000 - 0x6202d000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_extra.m32
0x65000000 - 0x6501a000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_net.m32
0x63000000 - 0x630a6000     C:\Program Files\BitDefender\BitDefender 2011\Active Virus Control\Midas_00078_002\plugin_fragments.m32
0x75d20000 - 0x75dc0000     C:\Windows\system32\ADVAPI32.dll
0x75dc0000 - 0x75e6c000     C:\Windows\system32\msvcrt.dll
0x76f20000 - 0x76f39000     C:\Windows\SYSTEM32\sechost.dll
0x754b0000 - 0x75551000     C:\Windows\system32\RPCRT4.dll
0x76cf0000 - 0x76db9000     C:\Windows\system32\USER32.dll
0x752a0000 - 0x752ee000     C:\Windows\system32\GDI32.dll
0x757f0000 - 0x757fa000     C:\Windows\system32\LPK.dll
0x76f40000 - 0x76fdd000     C:\Windows\system32\USP10.dll
0x73d70000 - 0x73f0e000     C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7600.16661_none_420fe3fa2b8113bd\COMCTL32.dll
0x75cc0000 - 0x75d17000     C:\Windows\system32\SHLWAPI.dll
0x75490000 - 0x754af000     C:\Windows\system32\IMM32.DLL
0x75800000 - 0x758cc000     C:\Windows\system32\MSCTF.dll
0x69700000 - 0x697be000     C:\Program Files\Java\jdk1.7.0\jre\bin\msvcr100.dll
0x65030000 - 0x65339000     C:\Program Files\Java\jdk1.7.0\jre\bin\client\jvm.dll
0x736c0000 - 0x736f2000     C:\Windows\system32\WINMM.dll
0x72860000 - 0x7286c000     C:\Program Files\Java\jdk1.7.0\jre\bin\verify.dll
0x70fc0000 - 0x70fe0000     C:\Program Files\Java\jdk1.7.0\jre\bin\java.dll
0x76f00000 - 0x76f05000     C:\Windows\system32\PSAPI.DLL
0x6f6b0000 - 0x6f6c3000     C:\Program Files\Java\jdk1.7.0\jre\bin\zip.dll
0x10000000 - 0x10259000     C:\Windows\System32\libspotify.dll
0x75f80000 - 0x75fb5000     C:\Windows\system32\WS2_32.dll
0x76f10000 - 0x76f16000     C:\Windows\system32\NSI.dll
0x74ff0000 - 0x7510c000     C:\Windows\system32\CRYPT32.dll
0x74f80000 - 0x74f8c000     C:\Windows\system32\MSASN1.dll
0x74410000 - 0x74468000     C:\Windows\system32\WINHTTP.dll
0x743c0000 - 0x7440f000     C:\Windows\system32\webio.dll
0x74990000 - 0x749a6000     C:\Windows\system32\CRYPTSP.dll
0x74730000 - 0x7476b000     C:\Windows\system32\rsaenh.dll
0x74570000 - 0x74587000     C:\Windows\system32\USERENV.dll
0x74f10000 - 0x74f1b000     C:\Windows\system32\profapi.dll
0x74e90000 - 0x74e9c000     C:\Windows\system32\CRYPTBASE.dll
0x6fbd0000 - 0x6fbe6000     C:\Program Files\Java\jdk1.7.0\jre\bin\net.dll
0x74950000 - 0x7498c000     C:\Windows\system32\mswsock.dll
0x74940000 - 0x74946000     C:\Windows\System32\wship6.dll
0x72d60000 - 0x72d70000     C:\Windows\system32\NLAapi.dll
0x74810000 - 0x74854000     C:\Windows\system32\DNSAPI.dll
0x70520000 - 0x70528000     C:\Windows\System32\winrnr.dll
0x70510000 - 0x70520000     C:\Windows\system32\napinsp.dll
0x704f0000 - 0x70502000     C:\Windows\system32\pnrpnsp.dll
0x74470000 - 0x74475000     C:\Windows\System32\wshtcpip.dll
0x74480000 - 0x7449c000     C:\Windows\system32\IPHLPAPI.DLL
0x741f0000 - 0x741f7000     C:\Windows\system32\WINNSI.DLL
0x701d0000 - 0x701d6000     C:\Windows\system32\rasadhlp.dll
0x72810000 - 0x72848000     C:\Windows\System32\fwpuclnt.dll
0x72d70000 - 0x72d7f000     C:\Program Files\Java\jdk1.7.0\jre\bin\nio.dll
0x04700000 - 0x04752000     C:\Users\nbarraille\AppData\Local\Temp\jna8472051172185323617.tmp

VM Arguments:
jvm_args: -Djava.library.path=C:\Windows\System32 -Dfile.encoding=Cp1252 
java_command: com.nbarraille.jspotify.main.Test
Launcher Type: SUN_STANDARD

Environment Variables:
PATH=C:/Program Files/Java/jdk1.7.0/bin/../jre/bin/client;C:/Program Files/Java/jdk1.7.0/bin/../jre/bin;C:/Program Files/Java/jdk1.7.0/bin/../jre/lib/i386;C:\Windows\System32
USERNAME=nbarraille
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 23 Stepping 10, GenuineIntel



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

OS: Windows 7 Build 7600 

CPU:total 2 (2 cores per cpu, 1 threads per core) family 6 model 23 stepping 10, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1

Memory: 4k page, physical 3624108k(899440k free), swap 7246452k(3244776k free)

vm_info: Java HotSpot(TM) Client VM (21.0-b01) for windows-x86 JRE (1.7.0-ea-b129), built on Feb 10 2011 07:43:39 by "java_re" with unknown MS VC++:1600

time: Tue Mar 01 22:57:31 2011
elapsed time: 0 seconds

我已经读过ExceptionCode 0xc00000005的EXCEPTION_ACCESS_VIOLATION通常是由于使用了一个包含无效内存地址的指针。你认为这是这种情况吗?我没有正确初始化会话实例吗?我该如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

是的,根据堆栈跟踪,sp_session_connectionstate函数取消引用NULL指针。

传递的sp_session可能是NULL。如果C代码不是你的,那很可能就是这种情况。

不幸的是,我对JNI不是很熟悉,所以我不知道你是否正确地将指针从Java传递给了C。其他人可能知道的更好。