Search code examples
javac++java-native-interfacememory-corruption

How to find errors in JNI


Updating some legacy code in our software and I have had to alter some of the calls through the JNI. However, after running it a few time the program consistently breaks in the same place in the c++ with an Access Violation Error, this transfers back to Java where a variable has been set to null, this cannot happen within the java (there is no way to set this variable null without cleaning up the class containing it) and the jni call where it breaks does not touch this portion of code.

Each JNI call checks for exceptions after it returns and my code is now full of assert(*someitem* != NULL); after retrieving each jclass and methodID and instantiating new java classes, none of them fail and as such I can only assume that the fault is happening somewhere in the native code and only picked up when returning to the java.

My question is, how can I find where the problem is actually occurring? I have used -xCheck:jni which shows nothing, -verbose:jni and -verbose:gc as suggested by a similar question, but to no avail.

Sorry no SSCCE is possible due to the nature of the problem


Solution

  • For anyone that finds this post and has the same issue I've finally fixed it.

    As it turned out I was using the wrong methodID in a call. I was reusing a variable that holds my methodIDs and at the time in question it was holding a constructor method that was valid for the object I was calling it on, this lead to no errors or exceptions however a crucial part of the program was missed.

    Hope this helps others if they have this terrible problem.