JNI EXCEPTION_ACCESS_VIOLATION

E

esandfort

I have a java application which calls into a VC++ DLL via JNI. Within
the DLL, I call some code via a 3rd party API which ultimately does
some disk IO. When the "File Writing" code is hit, my application
crashes. I get the following error (I'll spare you the entire dump)

An unexpected exception has been detected in native code outside the
VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at
PC=0x7C9111DE
Function=wcsncpy+0x74F
Library=C:\WINDOWS\system32\ntdll.dll

Here are a couple of interesting points...

The code will crash when running under XP Service Pack 2
The code will NOT crash when running under Windows 2000 Service Pack 4

When running on XP, the code will crash if the java progam makes either
of the following calls before making the JNI call:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- or -
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

When running on XP, the code will NOT crash if I omit the
setLookAndFeel() call all together, or use an alternate L&F (Metal for
example)

I have spent the past couple of days trying to find clues to this
problem on the newsgroups, and have had absolutely no luck.

If anyone has any experience with this problem, I would appreciate any
leads.

Also - if anyone could recommend any tools and/or techniques that would
be useful in debugging a VM crash such as I described above, that would
be appreciated as well.

Thanks!

Ed Sandfort
 
G

Gordon Beaton

I have a java application which calls into a VC++ DLL via JNI. Within
the DLL, I call some code via a 3rd party API which ultimately does
some disk IO. When the "File Writing" code is hit, my application
crashes. I get the following error (I'll spare you the entire dump)

An unexpected exception has been detected in native code outside the
VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at
PC=0x7C9111DE
Function=wcsncpy+0x74F
Library=C:\WINDOWS\system32\ntdll.dll

Here are a couple of interesting points...

You have an error in your native code that leaves the JVM in an
unstable state.

The fact that it appears to work correctly in some configurations is
not an indication that the code is ok.

Your error is likely due to slopping pointer handling, failure to
manage memory properly, uninitialized variables, failure to check
return values, and things like that. More information can be found
here:

http://c-faq.com/strangeprob/segv.html
http://c-faq.com/strangeprob/funnybugs.html
http://c-faq.com/malloc/crash.html

/gordon
 
B

Bill Medland

Gordon said:
You have an error in your native code that leaves the JVM in an
unstable state.

Of course. Microsoft NEVER make a mistake. Sun NEVER make a mistake. The
third party NEVER make a mistake. It can't possibly be any one else that
is wrong; it must be the OP.
The fact that it appears to work correctly in some configurations is
not an indication that the code is ok.

Your error is likely due to slopping pointer handling, failure to
manage memory properly, uninitialized variables, failure to check
return values, and things like that. More information can be found
here:

http://c-faq.com/strangeprob/segv.html
http://c-faq.com/strangeprob/funnybugs.html
http://c-faq.com/malloc/crash.html

/gordon

Based on my experience there is a 1% chance that the problem is not the OP.

I personally would expect that the problem is caused by the L&F resulting in
something becoming null where the 3rd party library doesn't handle that
possibility.

Honestly I would suggest the "if it hurts, don't do it" solution.
 
E

esandfort

Thanks Gordon,

Your point is well taken, but I'm fairly confident this is not the
problem. I experienced the error in a more complex system, and was
able to duplicate it with a VERY simple test program. It's unlikely
the test code contains are any of the errors you describe in your
response.

I should also mention that when the crash does happen, the last line of
code executed in my program seems to be somewhat randon according to
the print logs I include. It is as if the severe error occurs off in
another thread, and any correlation between the timing of the system
crash, and the line of my code that is currently being executed can't
be established with any regularity.

Thanks again!

Ed Sandfort
 
G

Gordon Beaton

Of course. Microsoft NEVER make a mistake. Sun NEVER make a mistake.
The third party NEVER make a mistake. It can't possibly be any one
else that is wrong; it must be the OP.
Based on my experience there is a 1% chance that the problem is not
the OP.

I agree, even if I think that 1% is somewhat high.

/gordon
 
?

=?iso-8859-1?q?In=E1cio_Ferrarini?=

Hi there.

I am not experienced (I never did) in coding a dll and access it via
Java, but, I would say to you that, if you do not mind, try to use the
same DLL via C++ or other windows native language, and, use the same
thing on the test app that your Java app uses, and, see if the crash
still occurs. This way, you will be able to isolate if the error is
internal of the dll or of the way you are using it in Java - even if
the error only happens sometimes under certain so quasai conditions -
but, as said before, it does not mean that the code is entirely ok.

Hope I Helped,
- Inácio Ferrarini
 
E

esandfort

Hello Inácio,

Thanks for the suggestion. If I understand you correctly, you are
suggesting that I access the C++ DLL's functions via a C++ program, to
take the JVM out of the equation. I have done this already, and this
scenario works fine without any crash.

I suspect there is some memory conflict condition that I am causing by
accessing the Windows XP disk read/write code (presumably in ntdll.dll
??????) running from within a JVM, and by calling the WindowLookAndFeel
code into play.

I have been researching this issue pretty rigorously for the past few
days, and have found other posting reporting similar problems.
Unfortunately, it seems that nobody has been able to offer a satisfying
explanation, and in my particular case, I have to use the Windows L&F,
so simply omitting it is not an option.

Short of a complete explanation on how to fix this problem, I could
really use advise on how to get more information from the crash that I
can use to track the problem down.

Thanks again for your post.

Ed Sandfort
 
Joined
Sep 19, 2006
Messages
1
Reaction score
0
Hi,
I am having the same problem! I have a C++ DLL that I did not write and do not have the header file for, which I need to access from Java, so I built a C++ Wrapper dll with JNI function headers which calls the the functions in the origional dll. Then I can call the functions in the wrapper dll from Java using native functions. All well and good, until it suddenly stopped working and spits out an EXCEPTION_ACCESS_VIOLATION. I stripped both Java and C++ down as basic as possible, but the problem is here to stay it seems. The error occurs during the C++ to C++ dll function call but only when the WrapperClass dll is called from JNI. Using C++ to call the functions in the wrapper class does not cause any problems or errors. Only when JNI is involved do things get sticky. I have tried everything I can think of, if no one here can help I will have to replace the C++ wrapper dll with a C++ application and use a socket to comunicate with Java! Thats like going squirrel-hunting with hand-grenades but I dont have a choice! I will throw both Java and C++ code in here, as well as the error-message. Please help! All ideas/recommendations will be thankfully received and followed-up-on!
Thanks in advance
Dawud Gordon

WrapperClass.java:

package javapackage;

public class WrapperClass {
static {
System.loadLibrary("CanApiWrapper");
}

private native int ResetCanCard();

public static void main(String[] args) {
javapackage.WrapperClass CanBus = new WrapperClass();
System.out.print(CanBus.ResetCanCard());
}
}

CanApiWrapper.cpp:


#include "stdafx.h"
#include "CanApiWrapper.h"
#include "fstream.h"


// DLL initialization function
BOOL APIENTRY
DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
return TRUE;
}




/****************************************************************************
*
* ResetCanCard
* Resets the can card
*
*****************************************************************************/

JNIEXPORT jint JNICALL Java_javapackage_WrapperClass_ResetCanCard(
JNIEnv *env,
jobject obj)
{

int RetVal;
typedef int (*ca_ResetCanCard_1_type) (void);
ca_ResetCanCard_1_type _ca_ResetCanCard_1;
HINSTANCE hinstLib;
hinstLib = LoadLibrary("canapi.dll");

if (hinstLib == NULL)
{
RetVal = -1;
}
else
{
try
{
_ca_ResetCanCard_1 = (ca_ResetCanCard_1_type)GetProcAddress(hinstLib, "ca_ResetCanCard_1");
if (_ca_ResetCanCard_1 != NULL)
{
//Error occurs when this line is executed
RetVal = _ca_ResetCanCard_1();
}
else
RetVal = -1;
}
catch (int a)
{
RetVal = -a;
}
}

FreeLibrary(hinstLib);
return (jint) RetVal;
}

CanApiWrapper.h:

#define _Included_WrapperClass

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_javapackage_WrapperClass_ResetCanCard
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


hs_err_pid1108.log:


#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000, pid=1108, tid=440
#
# Java VM: Java HotSpot(TM) Client VM (1.4.2_12-b03 mixed mode)
# Problematic frame:
# C 0x00000000
#

--------------- T H R E A D ---------------

Current thread (0x00735ce8): JavaThread "main" [_thread_in_native, id=440]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000

Registers:
EAX=0x00000000, EBX=0x140fdd48, ECX=0x0006fa44, EDX=0x778ce748
ESP=0x0006fa14, EBP=0x0006fa50, ESI=0x140fdd90, EDI=0x00735ce8
EIP=0x00000000, EFLAGS=0x00010206

Top of Stack: (sp=0x0006fa14)
0x0006fa14: 1829115c 00000000 1827106d 00735ce8
0x0006fa24: 140fdd90 140fdd48 080367ac 00736130
0x0006fa34: 18291150 08070341 18290000 0006fa20
0x0006fa44: 0006fb80 18275ee0 00000000 0006fa7c
0x0006fa54: 009f81a6 00735d84 0006fa8c 0006fa60
0x0006fa64: 140fdd90 0006fa8c 140fdf48 00000000
0x0006fa74: 140fdd48 0006fa8c 0006fab0 009f2d5f
0x0006fa84: 00000000 009f64a9 1002da38 10013ab0

Instructions: (pc=0x00000000)
0xfffffff0:


Stack: [0x00030000,0x00070000), sp=0x0006fa14, free space=254k
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j javapackage.WrapperClass.ResetCanCard()I+0
j javapackage.WrapperClass.main([Ljava/lang/String;)V+12
v ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
0x0093abc0 JavaThread "CompilerThread0" daemon [_thread_blocked, id=560]
0x00939e00 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=1012]
0x009373c8 JavaThread "Finalizer" daemon [_thread_blocked, id=1080]
0x00935ff0 JavaThread "Reference Handler" daemon [_thread_blocked, id=696]
=>0x00735ce8 JavaThread "main" [_thread_in_native, id=440]

Other Threads:
0x009795c8 VMThread [id=788]
0x0093c848 WatcherThread [id=708]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
def new generation total 576K, used 118K [0x10010000, 0x100b0000, 0x104f0000)
eden space 512K, 23% used [0x10010000, 0x1002dac0, 0x10090000)
from space 64K, 0% used [0x10090000, 0x10090000, 0x100a0000)
to space 64K, 0% used [0x100a0000, 0x100a0000, 0x100b0000)
tenured generation total 1408K, used 0K [0x104f0000, 0x10650000, 0x14010000)
the space 1408K, 0% used [0x104f0000, 0x104f0000, 0x104f0200, 0x10650000)
compacting perm gen total 4096K, used 952K [0x14010000, 0x14410000, 0x18010000)
the space 4096K, 23% used [0x14010000, 0x140fe060, 0x140fe200, 0x14410000)

Dynamic libraries:
0x00400000 - 0x0040b000 C:\Programme\Java\j2re1.4.2_12\bin\javaw.exe
0x77880000 - 0x77902000 C:\WINNT\system32\ntdll.dll
0x79350000 - 0x793b5000 C:\WINNT\system32\ADVAPI32.dll
0x77e70000 - 0x77f31000 C:\WINNT\system32\KERNEL32.dll
0x77d20000 - 0x77d8f000 C:\WINNT\system32\RPCRT4.dll
0x77e00000 - 0x77e69000 C:\WINNT\system32\USER32.dll
0x77f40000 - 0x77f7c000 C:\WINNT\system32\GDI32.dll
0x78000000 - 0x78045000 C:\WINNT\system32\MSVCRT.dll
0x08000000 - 0x08140000 C:\Programme\Java\j2re1.4.2_12\bin\client\jvm.dll
0x77540000 - 0x77571000 C:\WINNT\system32\WINMM.dll
0x10000000 - 0x10007000 C:\Programme\Java\j2re1.4.2_12\bin\hpi.dll
0x68f30000 - 0x68f3b000 C:\WINNT\system32\PSAPI.DLL
0x007e0000 - 0x007ee000 C:\Programme\Java\j2re1.4.2_12\bin\verify.dll
0x007f0000 - 0x00809000 C:\Programme\Java\j2re1.4.2_12\bin\java.dll
0x00810000 - 0x0081e000 C:\Programme\Java\j2re1.4.2_12\bin\zip.dll
0x18270000 - 0x1827c000 C:\JavaWorkspaces\NakedWC\WrapperClass\CanApiWrapper.dll
0x18290000 - 0x1834b000 C:\WINNT\system32\canapi.dll

VM Arguments:
java_command: javapackage.WrapperClass
Launcher Type: SUN_STANDARD

Environment Variables:
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;"C:\Programme\Symantec\Norton Ghost 2003\";C:\Dev-C++\Bin;C:\j2sdk1.4.2_12\bin;C:\Programme\Sontheim\MT_Api\;C:\Programme\Microsoft Visual Studio\Common\Tools\WinNT;C:\Programme\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Programme\Microsoft Visual Studio\Common\Tools;C:\Programme\Microsoft Visual Studio\VC98\bin;C:\j2sdk1.4.2_12\include;C:\j2sdk1.4.2_12\include\win32;C:\Programme\eclipse\plugins
USERNAME=gordon
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 13 Stepping 8, GenuineIntel


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

OS: Windows 2000 Build 2195 Service Pack 4

CPU:total 1 family 6, cmov, cx8, fxsr, mmx, sse, sse2

Memory: 4k page, physical 515440k(186768k free), swap 1256692k(975540k free)

vm_info: Java HotSpot(TM) Client VM (1.4.2_12-b03) for windows-x86, built on May 9 2006 12:30:51 by "java_re" with MS VC++ 6.0
 
Joined
Nov 3, 2006
Messages
1
Reaction score
0
Very Very Strange Problem

I have a very strange problem.
I have a c++ dll and I want to use it in my java program. So I created
a JNI dll to be used as a proxy between my java program and the dll.
Everythng seemed fine but here what happens. When I call a function
from the JNI it calles a wrong function from the dll and it causes
EXCEPTION_ACCESS_VIOLATION.

Here is some stuff to explain it better:
languageJNI.dll - the JNI dll
langugae.dll - the dll

loading language.dll...
finished loading language.dll
loading languageJNI.dll...
finished loading languageJNI.dll
languageJNI : creating interface...
language.dll : creating interface...
language.dll : interface created
inserting parameters...
language.dll : clearing configuration... /* this is wrong here should
be language.dll : inserting parameters... */

#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x1000cc88, pid=3064,
tid=3172
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode,
sharing)
# Problematic frame:
# C [language.dll+0xcc88]
#
# An error report file with more information is saved as
hs_err_pid3064.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#

Some code:

###################################################################

public class LanguageTrainApp {
public LanguageTrainApp() {
createInterface(0);
insertConfiguration(0, LanguageTrainApp.CORPUS_FILE, "README.txt");
insertConfiguration(0, LanguageTrainApp.WRITE_MODELDATA,
"model.txt");
execute(0);
releaseInterface(0);
}

static {
System.out.println("loading language.dll...");
System.loadLibrary("language");
System.out.println("finished loading language.dll");
System.out.println("loading languageJNI.dll...");
System.loadLibrary("languageJNI");
System.out.println("finished loading languageJNI.dll");
}

public static String CORPUS_FILE = "CORPUSFILE";
public static String WRITE_MODELDATA = "WRITE_MODELDATA";

private native void createInterface(int type);
private native void insertConfiguration(int id, String cmd, String
val);
private native String execute(int id);
private native void releaseInterface(int id);

public static void main(String[] args) {
new LanguageTrainApp();
}

}

#####################################################################

#include "LanguageTrainApp.h"
#include "knowledge.hh"
#include <iostream>
using namespace std;

LOGOS_KM_Interface* instance = NULL;

JNIEXPORT void JNICALL Java_LanguageTrainApp_createInterface
(JNIEnv *env, jobject object, jint type) {
cout << "languageJNI : creating interface..." << endl;
if (instance != NULL) {
delete instance;
instance = NULL;
}
instance = CreateLanguageTrain();

}

JNIEXPORT void JNICALL Java_LanguageTrainApp_insertConfiguration
(JNIEnv *env, jobject object, jint ID, jstring cmd, jstring val) {
const char *strCmd = env->GetStringUTFChars(cmd, 0);
const char *strVal = env->GetStringUTFChars(val, 0);
cout << "inserting parameters..." << endl;
instance->insert_configuration(strCmd, strVal);

}

JNIEXPORT jstring JNICALL Java_LanguageTrainApp_execute
(JNIEnv *env, jobject object, jint ID) {
cout << "languageJNI : configuring..." << endl;
instance->configure();

cout << "languageJNI : executing..." << endl;
instance->main();

cout << "languageJNI : getting result..." << endl;
jstring result = env->NewStringUTF(instance->get_result());

cout << "languageJNI : clearing configuration..." << endl;
instance->clear_configuration();

cout << "languageJNI : returning result..." << endl;
return result;

}

JNIEXPORT void JNICALL Java_LanguageTrainApp_releaseInterface
(JNIEnv *env, jobject object, jint ID) {
delete instance;
instance = NULL;

}

##################################################################

#include "language_train_interface.h"
#include <iostream>
using namespace std;

using namespace km;

LanguageInterface::LanguageInterface() {
cout << "language.dll : creating interface..." << endl;
_languagetrain = new LanguageTrainApp();
_map_return_values = new MapReturnValues();
cout << "language.dll : interface created" << endl;

}

LanguageInterface::~LanguageInterface() {
cout << "language.dll : destroying interface..." << endl;
delete _languagetrain;
delete _map_return_values;
cout << "language.dll : interface destroyed" << endl;

}

void LanguageInterface::insert_configuration(const char* parameter,
const char* value) {
cout << "language.dll : inserting parameters..." << endl;
_languagetrain->insert_configuration(string(parameter),
string(value));
cout << "language.dll : parameters inserted" << endl;

}

void LanguageInterface::clear_configuration() {
cout << "language.dll : clearing configuration..." << endl;
_languagetrain->clear_configuration();
cout << "language.dll : configuration cleared" << endl;

}

#ifdef AIXPLAIN_ENABLE_READ_CONFIGURATION
bool LanguageInterface::read_configuration(const char* filename, bool
error) {
cout << "language.dll : reading configuration..." << endl;
return _languagetrain->read_configuration(string(filename), error);
}

#endif // AIXPLAIN_ENABLE_READ_CONFIGURATION

void LanguageInterface::configure () {
cout << "language.dll : configuring..." << endl;
_languagetrain->configure();
cout << "language.dll : configured" << endl;

}

const char* LanguageInterface::get_configuration() {
cout << "language.dll : getting configuration..." << endl;
string s = _languagetrain->get_configuration();
char* p = new char[s.length() +1];
s.copy(p, string::npos);
p[s.length()] = 0;
return p;

}

int LanguageInterface::main() {
cout << "language.dll : executing..." << endl;
return _map_return_values->application2knowledge(
_languagetrain->main() );

}

const char* LanguageInterface::get_result() {
cout << "language.dll : getting result..." << endl;
string s = _languagetrain->get_result();
char* p = new char[s.length() +1];
s.copy(p, string::npos);
p[s.length()] = 0;
return p;

}

LOGOS_KM_Interface* CreateLanguageTrain() {
return new LanguageInterface();

}

#####################################################

Please help. This is so strange for me! Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top