From 58efee8be21f2c21b1cf2db34e15cf9d163fcbed Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Fri, 25 Apr 2014 18:03:27 -0400 Subject: [PATCH] Avoid StrictMode error in OpenPgpApi E/StrictMode(9278): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. E/StrictMode(9278): java.lang.Throwable: Explicit termination method 'close' not called E/StrictMode(9278): at dalvik.system.CloseGuard.open(CloseGuard.java:184) E/StrictMode(9278): at android.os.ParcelFileDescriptor.(ParcelFileDescriptor.java:179) E/StrictMode(9278): at android.os.ParcelFileDescriptor.(ParcelFileDescriptor.java:168) E/StrictMode(9278): at android.os.ParcelFileDescriptor.createPipe(ParcelFileDescriptor.java:362) E/StrictMode(9278): at org.openintents.openpgp.util.ParcelFileDescriptorUtil.pipeFrom(ParcelFileDescriptorUtil.java:34) E/StrictMode(9278): at org.openintents.openpgp.util.OpenPgpApi.executeApi(OpenPgpApi.java:222) E/StrictMode(9278): at org.openintents.openpgp.util.OpenPgpApi$OpenPgpAsyncTask.doInBackground(OpenPgpApi.java:189) E/StrictMode(9278): at org.openintents.openpgp.util.OpenPgpApi$OpenPgpAsyncTask.doInBackground(OpenPgpApi.java:1) E/StrictMode(9278): at android.os.AsyncTask$2.call(AsyncTask.java:288) E/StrictMode(9278): at java.util.concurrent.FutureTask.run(FutureTask.java:237) E/StrictMode(9278): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) E/StrictMode(9278): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) E/StrictMode(9278): at java.lang.Thread.run(Thread.java:841) --- .../org/openintents/openpgp/util/OpenPgpApi.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/openpgp-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java b/plugins/openpgp-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java index 465a12002..5c3c15f11 100644 --- a/plugins/openpgp-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java +++ b/plugins/openpgp-api-library/src/org/openintents/openpgp/util/OpenPgpApi.java @@ -25,6 +25,8 @@ import android.os.ParcelFileDescriptor; import android.util.Log; import org.openintents.openpgp.IOpenPgpService; import org.openintents.openpgp.OpenPgpError; + +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -209,6 +211,7 @@ public class OpenPgpApi { } public Intent executeApi(Intent data, InputStream is, OutputStream os) { + ParcelFileDescriptor input = null; try { data.putExtra(EXTRA_API_VERSION, OpenPgpApi.API_VERSION); @@ -219,7 +222,7 @@ public class OpenPgpApi { return result; } else { // pipe the input and output - ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is, + input = ParcelFileDescriptorUtil.pipeFrom(is, new ParcelFileDescriptorUtil.IThreadListener() { @Override @@ -255,6 +258,14 @@ public class OpenPgpApi { result.putExtra(RESULT_ERROR, new OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.getMessage())); return result; + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + Log.e(OpenPgpApi.TAG, "Failed to close input file descriptor", e); + } + } } }