When is jni onload called




















Vendors can deliver Java-enabled applications without having to link with the Java VM source code. This chapter begins with an overview of the Invocation API.

This is followed by reference pages for all Invocation API functions. It covers the following topics:. The following code example illustrates how to use functions in the Invocation API. For clarity, we omit error checking. This example uses three functions in the API. Once attached to the VM, a native thread works just like an ordinary Java thread running inside a native method. The attached thread should have enough stack space to perform a reasonable amount of work.

The allocation of stack space per thread is operating system-specific. A thread cannot detach itself if there are Java methods on the call stack. The VM waits until the current thread is the only non-daemon user thread before it actually unloads. User threads include both Java threads and attached native threads.

This restriction exists because a Java thread or attached native thread may be holding system resources, such as locks, windows, and so on. The call wrappers conform to the signature of the JNI Call Each method that can be called via JNI has a reflection metadata object. Because each call wrapper conforms precisely to the corresponding Call Native Image supports both approaches. Instead of generating additional call wrappers for NewObject , the regular CallVoidMethod wrapper is reused and detects when it is called via NewObject because it is passed the Class object of the target class.

In that case, the call wrapper allocates a new instance before invoking the actual constructor. For a field that is accessible via JNI, its offset within an object or within the static field area is stored in the reflection metadata and is used as its jfieldID.

The native image builder generates accessor methods for fields of all primitive types and for object fields. These accessor methods perform the transition to Java code and back, and use unsafe loads or stores to directly manipulate the field value.

JNI also permits writing fields that are declared final , which must be enabled for individual fields with an allowWrite property in the configuration file. However, code accessing final fields might not observe changes of final field values in the same way as for non-final fields because of optimizations.

JNI specifies that exceptions in Java code that are the result of a call from native code must be caught and retained. Native code can then query and clear a pending exception with the ExceptionCheck , ExceptionOccurred , ExceptionDescribe , and ExceptionClear functions. When an exception remains unhandled in native code or the native code itself throws an exception, the Java-to-native call wrapper rethrows that exception upon reentering Java code.

If you attach a native thread with AttachCurrentThread , the code you are running will never automatically free local references until the thread detaches.

Any local references you create will have to be deleted manually. In general, any native code that creates local references in a loop probably needs to do some manual deletion. Be careful using global references. Global references can be unavoidable, but they are difficult to debug and can cause difficult-to-diagnose memory mis behaviors. All else being equal, a solution with fewer global references is probably better. The Java programming language uses UTF The nice thing about this is that you can count on having C-style zero-terminated strings, suitable for use with standard libc string functions.

If possible, it's usually faster to operate with UTF strings. Don't forget to Release the strings you Get. They are guaranteed valid until Release is called, which means they are not released when the native method returns. A common mistake is reading character data from a file or network stream and handing it to NewStringUTF without filtering it.

If you don't, the UTF conversion is likely to provide unexpected results. CheckJNI—which is on by default for emulators—scans strings and aborts the VM if it receives invalid input. JNI provides functions for accessing the contents of array objects. While arrays of objects must be accessed one entry at a time, arrays of primitives can be read and written directly as if they were declared in C. Either way, the raw pointer returned is guaranteed to be valid until the corresponding Release call is issued which implies that, if the data wasn't copied, the array object will be pinned down and can't be relocated as part of compacting the heap.

You must Release every array you Get. You can determine whether or not the data was copied by passing in a non-NULL pointer for the isCopy argument.

This is rarely useful. The Release call takes a mode argument that can have one of three values. The actions performed by the runtime depend upon whether it returned a pointer to the actual data or a copy of it:.

For example, you might want to get an array, modify it in place, pass pieces to other functions, and then discard the changes. If you know that JNI is making a new copy for you, there's no need to create another "editable" copy.

If JNI is passing you the original, then you do need to make your own copy. This is not the case. If no copy buffer was allocated, then the original memory must be pinned down and can't be moved by the garbage collector. Consider the following:. This grabs the array, copies the first len byte elements out of it, and then releases the array. Depending upon the implementation, the Get call will either pin or copy the array contents.

Exceptions You must not call most JNI functions while an exception is pending. Your code is expected to notice the exception via the function's return value, ExceptionCheck , or ExceptionOccurred and return, or clear the exception and handle it. Many JNI calls can throw an exception, but often provide a simpler way of checking for failure.

However, if you call a method using a function like CallObjectMethod , you must always check for an exception, because the return value is not going to be valid if an exception was thrown. Upon returning to managed from native code, the exception will be noted and handled appropriately.

Native code can "catch" an exception by calling ExceptionCheck or ExceptionOccurred , and clear it with ExceptionClear. As usual, discarding exceptions without handling them can lead to problems. JNI does very little error checking. Errors usually result in a crash. Android also offers a mode called CheckJNI, where the JavaVM and JNIEnv function table pointers are switched to tables of functions that perform an extended series of checks before calling the standard implementation.

Accessibility of methods and fields is still not checked: access restrictions don't apply to native code. If you have a rooted device, you can use the following sequence of commands to restart the runtime with CheckJNI enabled:. Change the property to any other value or simply rebooting will disable CheckJNI again. You can also set the android:debuggable attribute in your application's manifest to turn on CheckJNI just for your app. Note that the Android build tools will do this automatically for certain build types.

Jnidemo The information printed out here is very important , Mainly signature information. Dangling pointer? This article will help you understand!



0コメント

  • 1000 / 1000