• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

JNI help

I'm encountering a problem when I try to use a C DLL that has a Win32 SOCKET object.  When I open and close a connection within one JNI method call, it works fine.  But when I open a connection in one JNI call and close on a different JNI call, the JVM is crashes.  This is the error that I get:

An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION occurred at PC=0x102141EB

Does anybody know what the problem is, and what possible solutions/work-arounds there are?
  • 2
1 Solution
Sounds like you aren't using the correct SOCKET object to close it. How do you maintain the reference to the instance between calls?
RadfordAuthor Commented:
The Win32 SOCKET object is actually inside an object in some third party code.  The third party code has open and close functions.  Again, calling these functions within one JNI call works fine, but in seperate JNI calls, it crashes.  The Open function in the third party code returns a reference to the object that contains the SOCKET object.  I am assigning that object to a global variable in the C code.
You should read the documentation for that socket class. For example, MFC class CSocket is not thread safe. If you pass CSocket object between threads the program crashes. It is recomended to use CSocket.Attach() and CSocket.Detach() methods. So check for these or similar methods. The scenario is as follows():

1. You should keep SOCKET handle, not the socket object (CSocket for example) as global variable!
2. When you call some JNI method, attach the SOCKET handle to the  socket object and this way you have a high level socket wrapping helper!
3. At the and of the method, detach the SOCKET handle from the socket object!

I think this will help you!
RadfordAuthor Commented:
alright my colleague and i solved the problem on our own.  =)

i'll give the points to msterjev for trying to help.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now