From eeffd8b996051cc765dc389c8ca43d11692528fb Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Thu, 15 Sep 2016 20:03:03 +0100 Subject: Android JNI context fixes. In the JNI code, we attach a cloned context onto each thread we encounter in thread local storage. When the thread shuts down, we should destroy that context. This can theoretically be achieved on pthreads by using the destructor registered to the tls slot. I have yet to see Android ever actually call this destructor yet though. No such mechanism exists for windows thread, so we'll just leak here for now. There is a potential fix for this, but it's hairy, so details are left in a comment in the code. --- platform/java/mupdf_native.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'platform/java') diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c index 823846bc..5723f4de 100644 --- a/platform/java/mupdf_native.c +++ b/platform/java/mupdf_native.c @@ -618,16 +618,30 @@ static void fin_base_context(JNIEnv *env) base_context = NULL; } +#ifndef _WIN32 +static void drop_tls_context(void *arg) +{ + fz_context *ctx = (fz_context *)arg; + + fz_drop_context(ctx); +} +#endif + static int init_base_context(JNIEnv *env) { int i; #ifdef _WIN32 + /* No destructor on windows. We will leak contexts. + * There is no easy way around this, but this page: + * http://stackoverflow.com/questions/3241732/is-there-anyway-to-dynamically-free-thread-local-storage-in-the-win32-apis/3245082#3245082 + * suggests a workaround that we can implement if we + * need to. */ context_key = TlsAlloc(); if (context_key == TLS_OUT_OF_INDEXES) return -1; #else - pthread_key_create(&context_key, NULL); + pthread_key_create(&context_key, drop_tls_context); #endif for (i = 0; i < FZ_LOCK_MAX; i++) -- cgit v1.2.3