JNI local references and the NDK

416 views
Skip to first unread message

foo64

unread,
Feb 28, 2013, 1:56:24 PM2/28/13
to andro...@googlegroups.com
I'm pretty clear on the usage of local and global references in JNI. My question is how this interacts with NDK functions that take a JNI reference and return an internal variable:

/**
 * Return the ANativeWindow associated with a Java Surface object,
 * for interacting with it through native code.  This acquires a reference
 * on the ANativeWindow that is returned; be sure to use ANativeWindow_release()
 * when done with it so that it doesn't leak.
 */
ANativeWindow* ANativeWindow_fromSurface(JNIEnv* env, jobject surface);

I assume this acquire/release mechanism is separate from JNI? For example, this code would run fine:

    jobject surfaceLocalRef = ...
    ANativeWindow* nativeWindow = ANativeWindow_fromSurface( env, surfaceLocalRef );
    env->DeleteLocalRef( surfaceLocalRef );
    // keep using nativeWindow until you call ANativeWindow_release( )

Basically, is the lifetime of the ANativeWindow*  separate form the lifetime of the JNI reference used to retrieve it?

gadget

unread,
Mar 1, 2013, 6:22:14 PM3/1/13
to andro...@googlegroups.com
You don't need to hang on to the surfaceLocalRef after you do ANativeWindow_fromSurface. You do need to call ANativeWindow_release() though.

foo64

unread,
Mar 3, 2013, 4:03:37 PM3/3/13
to andro...@googlegroups.com
So then this one is inconsistent, as it needs a reference:

/**
 * Given a Dalvik AssetManager object, obtain the corresponding native AAssetManager
 * object.  Note that the caller is responsible for obtaining and holding a VM reference
 * to the jobject to prevent its being garbage collected while the native object is
 * in use.
 */
AAssetManager* AAssetManager_fromJava(JNIEnv* env, jobject assetManager);

gadget

unread,
Mar 4, 2013, 11:22:22 AM3/4/13
to andro...@googlegroups.com
yeah, asset manager seems to be different
Reply all
Reply to author
Forward
0 new messages