Santosh Tiwari
Santosh Tiwari

Reputation: 1216

java jni Exception Access Violation

My java application which uses JNI is crashing with hs_err_pid file giving the error as "Exception Access Violation". The OS is Windows VISTA.

From what I know, my native code is illegally writing to some chunk of memory that does not belong to it.

I have used valgrind on Linux on pure native code to detect such problems in the past.

But when using java, valgrind simply fails and does not work.

What (if any) method would you suggest to identify the offending piece of code?

It is not possible for me to manually dig through the native code (few million lines) to identify it.

Upvotes: 0

Views: 3326

Answers (3)

Lukasz Czerwinski
Lukasz Czerwinski

Reputation: 15452

For debugging JNI code a method posted in this article could be useful (it's about debugging JNI using Netbeans and Visual Studio). It's simple - just start your Java program, then in Visual Studio pick Debug -> Attach to process and choose java.exe process running your program.

When you add breakpoints to your C++ code, Visual Studio will break on them. Voila :)

Upvotes: 0

Santosh Tiwari
Santosh Tiwari

Reputation: 1216

I was finally able to resolve the issue. I thought I will post the procedure here in case someone else is in a similar situation.

Step 1: Build the native code with proper debugging symbols. The compiler flags could be something like

"-g -rdynamic -O0"
.

Step 2: The following valgrind command should do the job.


valgrind --error-limit=no --trace-children=yes --smc-check=all --leak-check=full --track-origins=yes -v $JAVA -XX:UseSSE=0 -Djava.compiler=NONE $JAVA_ARGS

In the above command, $JAVA is the java executable and $JAVA_ARGS is the arguments to your java program.

Once successfully started, it will take orders of magnitude more time to complete the execution. Valgrind will print thousands of errors (most related to jvm which can be ignored). You can however identify the ones that relate to your jni code.

This general strategy should be applicable to most native memory related problems.

Upvotes: 5

Flavio
Flavio

Reputation: 11977

If you are running Java under Linux, you could use the -XX:OnError="gdb - %p" option to run gdb when the error occurs. See this example.

Under windows, you can use the -XX:+UseOSErrorReporting option to obtain a similar effect.

Upvotes: 2

Related Questions