Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Linked Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.
Accept all cookies Customize settings. Thread object to be constructed and added to the "main" ThreadGroup , making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread is a no-op. Android does not suspend threads executing native code.
If garbage collection is in progress, or the debugger has issued a suspend request, Android will pause the thread the next time it makes a JNI call. If coding this directly is awkward, in Android 2. Similarly, to call a method, you'd first get a class object reference and then a method ID.
The IDs are often just pointers to internal runtime data structures. Looking them up may require several string comparisons, but once you have them the actual call to get the field or invoke the method is very quick.
If performance is important, it's useful to look the values up once and cache the results in your native code. Because there is a limit of one JavaVM per process, it's reasonable to store this data in a static local structure. The class references, field IDs, and method IDs are guaranteed valid until the class is unloaded. Classes are only unloaded if all classes associated with a ClassLoader can be garbage collected, which is rare but will not be impossible in Android.
Note however that the jclass is a class reference and must be protected with a call to NewGlobalRef see the next section. If you would like to cache the IDs when a class is loaded, and automatically re-cache them if the class is ever unloaded and reloaded, the correct way to initialize the IDs is to add a piece of code that looks like this to the appropriate class:.
The code will be executed once, when the class is initialized. If the class is ever unloaded and then reloaded, it will be executed again.
Every argument passed to a native method, and almost every object returned by a JNI function is a "local reference". This means that it's valid for the duration of the current native method in the current thread. Even if the object itself continues to live on after the native method returns, the reference is not valid.
This applies to all sub-classes of jobject , including jclass , jstring , and jarray. The runtime will warn you about most reference mis-uses when extended JNI checks are enabled. If you want to hold on to a reference for a longer period, you must use a "global" reference. The NewGlobalRef function takes the local reference as an argument and returns a global one.
The global reference is guaranteed to be valid until you call DeleteGlobalRef. This pattern is commonly used when caching a jclass returned from FindClass , e. All JNI methods accept both local and global references as arguments. It's possible for references to the same object to have different values. For example, the return values from consecutive calls to NewGlobalRef on the same object may be different.
To see if two references refer to the same object, you must use the IsSameObject function. One consequence of this is that you must not assume object references are constant or unique in native code. The bit value representing an object may be different from one invocation of a method to the next, and it's possible that two different objects could have the same bit value on consecutive calls. Do not use jobject values as keys. Programmers are required to "not excessively allocate" local references.
In practical terms this means that if you're creating large numbers of local references, perhaps while running through an array of objects, you should free them manually with DeleteLocalRef instead of letting JNI do it for you.
They may be passed between threads, and are valid until the matching Release call. One unusual case deserves separate mention. 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. The sender takes no responsibility for any unauthorized reliance on this message. If you have received this message in error, please immediately notify the sender and delete the message you received. Please do not forward this message without permission. So what's the official JNI spec if there is one?
The "Java Native Interface Specification" I linked on the Oracle page, the book you linked, or some combination of the two?
ReleaseStringChars releases the pointer to the array of Unicode characters. NewString constructs a new java. String object from an array of Unicode characters.
0コメント