JNI/Win32 SOCKET help

Posted on 2003-03-27
Medium Priority
Last Modified: 2006-11-17
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?
Question by:Radford
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 8220535
>>Does anybody know what the problem is

Without code - no.

Expert Comment

ID: 8223046
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.


Accepted Solution

Kocil earned 1000 total points
ID: 8223053
Independent Software Vendors: 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!


Author Comment

ID: 8223092
>> 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

Expert Comment

ID: 8223116
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.


Author Comment

ID: 8223402
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.

Author Comment

ID: 8225677
alright my colleague and i solved the problem on our own.  =)

i'll just give the points to Kocil anyway for trying to help.  

Expert Comment

ID: 8228597
> 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 :)

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question