Debugging Java Native Interface (JNI) DLLs using Microsoft Visual C++ 2008 Express and NetBeans 6.9
Sometimes your JNI DLLs will crash, leaving you with the fun task of
figuring out why. If you have access to the native source code, this
proves to be pretty easy.
First, you need to create the Java application and JNI DLL in the usual
manner. The process of creating the source code for the JNI is not
covered here, but the project files for the examples used to create the
screenshots can be obtained by downloading the following zip file.
In your NetBeans project directory, you will notice that the dist
directory containing the Java application also contains the JNI DLL and
Program Debug Database (pdb) file, which is needed to map errors to the
corresponding source code. You have to copy these two files by hand to
the dist directory after building them in VS2008 Express. In a real
environment, these would hopefully be built by continuous integration.
In NetBeans, run the Java application. The following should appear:
In VS2008 Express, with the DLL project open, go to the Debug -> Attach
to Process menu option and attach to the running copy of the Java
By default in VS2008 Express, only a select number of exceptions are caught by
default by the debugger. However, you want to make sure all exceptions are caught,
so make sure that the checkmark next to "Win32 Exceptions" is fully black-and-white
and not gray.
In the Java application, click on the Test Bad Function button.
When the badFunction is called, and it attempts to write to an invalid memory
location, the Visual Studio debugger will catch this exception and show you the
line of code responsible.
This should provide you the information needed to implement a fix.