3 messages in net.java.dev.jna.usersRe: [jna-users] Exception in JNA fina...
FromSent OnAttachments
Yury KudryashovApr 23, 2009 7:08 am 
Timothy WallApr 23, 2009 10:45 am 
Yury KudryashovApr 24, 2009 12:54 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Exception in JNA finalizerActions...
From:Timothy Wall (twal@dev.java.net)
Date:Apr 23, 2009 10:45:34 am
List:net.java.dev.jna.users

What happens if you keep a single structure reference and always pass in the same one? What happens if you turn off autoread/write on the structures?

It's entirely possible your native code is corrupting the memory, since JNA won't write outside the bounds it's allocated. Does your JNA structure size match that expected by the native code?

On Apr 23, 2009, at 10:08 AM, Yury Kudryashov wrote:

Hi,

I'm using a proprietary DLL via JNA. All works great except for a single problem described below.

The DLL communicates with the remote server and allows retrieving huge list of entitites (more than 75000, each is about 350 bytes of size in C). The API is like this: there is a method to get the number of entities:

INT WINAPI countEntities(PINT count)

and the other one to get an entity by index:

INT WINAPI getEntity(int index, EntityStructPtr s);

where EntityStructPtr is a pointer to a complex structure.

I have mapped these functions and the structure to JNA ok, everything works fine with small data sets and when I use a test DLL (that provides the same interface). But when it comes to real DLL, I get JVM crashes with the following:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x775059c3, pid=3304, tid=3612

Stack trace is:

Stack: [0x1f320000,0x1f370000], sp=0x1f36f780, free space=317k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) C [ntdll.dll+0x659c3] C [ntdll.dll+0x65883] C [kernel32.dll+0x4c56f] C [msvcrt.dll+0x9d6b] C [jna7098527778289589434.tmp+0x6012] j com.sun.jna.Memory.free(J)V+0 j com.sun.jna.Memory.finalize()V+13 v ~StubRoutines::call_stub

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code) j com.sun.jna.Memory.free(J)V+0 j com.sun.jna.Memory.finalize()V+13 v ~StubRoutines::call_stub j java.lang.ref.Finalizer.invokeFinalizeMethod(Ljava/lang/Object;)V+0 j java.lang.ref.Finalizer.runFinalizer()V+45 j java.lang.ref.Finalizer.access$100(Ljava/lang/ref/Finalizer;)V+1 j java.lang.ref.Finalizer$FinalizerThread.run()V+11 v ~StubRoutines::call_stub

The crashes occur not immediately, but after some number of entities are retrieved ok. It seems that this error happens when JNA tries to free memory for the structures passed to getEntity. I do not use them and it seems they should be GCed ok.

I tried to find a workaround and tried to store references to the structures to avoid GC - but this soon leads to OutOfMemory errors (although almost all entities are loaded ok).

Looking forward to any ideas.

Thank you. Yury Kudryashov.