JNI checklist
There are a number of items that you must remember when using the JNI.
The following table shows the JNI checklist:
Remember | Outcome of nonadherence |
---|---|
Local references cannot be saved in global variables. | Random crashes (depending on what you pick up in the overwritten object space) happen at random intervals. |
Ensure that every global reference created has a path that deletes that global reference. | Memory leak. It might throw a native exception if the global reference storage overflows. It can be difficult to isolate. |
Always check for exceptions (or return codes) on return from a JNI function. Always handle a deferred exception immediately you detect it. | Unexplained exceptions or undefined behavior. Might crash the JVM. |
Ensure that array and string elements are always freed. | A small memory leak. It might fragment the heap and cause other problems to occur first. |
Ensure that you use the isCopy and mode flags correctly. See Copying and pinning. | Memory leaks, heap fragmentation, or both. |
When you update a Java™ object in native code, ensure synchronization of access. | Memory corruption. |