JNI/Win32 SOCKET 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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>>Does anybody know what the problem is

Without code - no.
That's because the socket's handle you get from the first method (open) is invalid at the second method (close).

Each JNI method is runs on its own context (process segment). On the first method, when you open a socket you get the handle that is valid on the first context.
When you call the other method, it is executed on another context, and the socket handle (which is relative to the first context) will refer to the area outside the second context. So ... access violation happens.

I wonder why do you need to do socket operation with native C++ ? Java has net packages that will do that nice and portable.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

RadfordAuthor Commented:
>> I wonder why do you need to do socket operation with
>> native C++ ? Java has net packages that will do that
>> nice and portable.

Why does anybody need to use JNI?  :)
Because there's already C code written to do what we need to do and we don't want to re-write the entire thing in Java.  Anyway thanks for the help.  I'll look into those links you mentioned and if they're any help, I'll give you the points.

- r
Kocil, please cite references for your 'theories' regarding memory management with JNI.  IIRC, What you are saying is false (or at least worded strangely to me.)  If you need to share variables across function calls, obviously you can't use locals.  However, if you have an object that you create when dllmain is called attaching to the dll, that object will remain valid until you delete it or the dll is unloaded.

RadfordAuthor Commented:
to clarify, the Win32 SOCKET object is actually contained in third-party code that we use.  it has an Open and Close function that returns a reference to a third party object that contains the SOCKET object.  perhaps the problem is not in the SOCKET object itself, but the third party code, or the way that the JVM is handling the dll, but i can't see how it's a problem with the third party code since as far as it is concerned, it is just being called regularly by C code.  it doesn't know that a JVM is calling it.
RadfordAuthor Commented:
alright my colleague and i solved the problem on our own.  =)

i'll just give the points to Kocil anyway for trying to help.  
> that object will remain valid until you delete it or the dll is unloaded.

I thought he has 2 methods on 2 different DLLs.
Thanks for the points :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.