Detach from JNI during shutdown of the UI thread on Android (flutter/engine#5120)

The APKAssetProvider will hold a reference to its Java peer
(see c2b081e229)

After dropping this reference, the UI thread must detach from JNI before exiting.
This commit is contained in:
Jason Simmons
2018-04-30 10:44:01 -07:00
committed by GitHub
parent 9e955689ea
commit 8c9ef95bea
2 changed files with 22 additions and 1 deletions

View File

@@ -6,6 +6,7 @@
#include "flutter/shell/platform/android/android_shell_holder.h"
#include <pthread.h>
#include <sys/resource.h>
#include <sys/time.h>
@@ -27,9 +28,18 @@ AndroidShellHolder::AndroidShellHolder(
static size_t shell_count = 1;
auto thread_label = std::to_string(shell_count++);
FXL_CHECK(pthread_key_create(&thread_destruct_key_, ThreadDestructCallback) ==
0);
thread_host_ = {thread_label, ThreadHost::Type::UI | ThreadHost::Type::GPU |
ThreadHost::Type::IO};
// Detach from JNI when the UI thread exits.
thread_host_.ui_thread->GetTaskRunner()->PostTask(
[key = thread_destruct_key_]() {
FXL_CHECK(pthread_setspecific(key, reinterpret_cast<void*>(1)) == 0);
});
fml::WeakPtr<PlatformViewAndroid> weak_platform_view;
Shell::CreateCallback<PlatformView> on_create_platform_view =
[java_object, &weak_platform_view](Shell& shell) {
@@ -80,7 +90,15 @@ AndroidShellHolder::AndroidShellHolder(
}
}
AndroidShellHolder::~AndroidShellHolder() = default;
AndroidShellHolder::~AndroidShellHolder() {
shell_.reset();
thread_host_.Reset();
FXL_CHECK(pthread_key_delete(thread_destruct_key_) == 0);
}
void AndroidShellHolder::ThreadDestructCallback(void* value) {
fml::jni::DetachFromVM();
}
bool AndroidShellHolder::IsValid() const {
return is_valid_;

View File

@@ -50,6 +50,9 @@ class AndroidShellHolder {
ThreadHost thread_host_;
std::unique_ptr<Shell> shell_;
bool is_valid_ = false;
pthread_key_t thread_destruct_key_;
static void ThreadDestructCallback(void* value);
FXL_DISALLOW_COPY_AND_ASSIGN(AndroidShellHolder);
};