Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When our JNI support attaches to the current thread, it should have a balancing DetachCurrentThread #557

Closed
mkustermann opened this issue Nov 2, 2023 · 0 comments · Fixed by #1163

Comments

@mkustermann
Copy link
Member

To me it seems we are lazily attaching to the current thread and caching the result in a thread_local JNIEnv* but there's no corresponding call to DetachCurrentThread.

See JNI tips:

"""
Threads attached through JNI must call DetachCurrentThread() before they exit. If coding this directly is awkward, in Android 2.0 (Eclair) and higher you can use pthread_key_create() to define a destructor function that will be called before the thread exits, and call DetachCurrentThread() from there. (Use that key with pthread_setspecific() to store the JNIEnv in thread-local-storage; that way it'll be passed into your destructor as the argument.)
"""

So it seems that we

  • should first detect whether current thread is attached
  • use AttachCurrentThread() which may be a NOP if it's already attached
  • if we were not attached before, we should use the pthread key approach mentioned above to install a destructor that calls DetachCurrentThread()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants