Reputation: 20195
Why does my Android app crash when my threads exit?
In logcat
I see the following output when my app crashes.
D/dalvikvm: threadid=13: thread exiting, not yet detached (count=0)
D/dalvikvm: threadid=13: thread exiting, not yet detached (count=1)
E/dalvikvm: threadid=13: native thread exited without detaching
E/dalvikvm: VM aborting
I am not sure what thread causes this. The app makes use of a C++ library with a few instances of STL threads (std::thread
). Here is an example of how I work with threads in the library:
std::thread thread([context] () { ... });
thread.detach();
The library works without any such errors on iOS, OS X, and Linux. My guess is that the error is not because of a missing call to std::thread::detach
.
Not sure if it is relevant:
gnustl_static
JavaVM::AttachCurrentThread
or JavaVM::DetachCurrentThread
I am not sure what to do or what to look for.
Upvotes: 4
Views: 3093
Reputation: 52313
Well, something is calling AttachCurrentThread
. If nothing were, the VM wouldn't be aware if the thread exited or not.
Any thread that makes JNI calls must first be attached to the VM. Once attached, it must detach before exiting to avoid resource leaks.
This post has more information about the situation, and this post has some additional details.
Upvotes: 4