[Android] VLCCrashHandler: do not leak resources

Jean-Baptiste Kempf git at videolan.org
Fri Jul 18 21:55:21 CEST 2014


vlc-ports/android | branch: master | Jean-Baptiste Kempf <jb at videolan.org> | Fri Jul 18 21:47:16 2014 +0200| [f1069517561433c33e4f53572c7863562bba3ad0] | committer: Jean-Baptiste Kempf

VLCCrashHandler: do not leak resources

And yes, the try{} catch in the finally is necessary, because .close()
can resend an IOException.

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=f1069517561433c33e4f53572c7863562bba3ad0
---

 .../src/org/videolan/vlc/VLCCrashHandler.java      |   24 +++++++++++++++-----
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/VLCCrashHandler.java b/vlc-android/src/org/videolan/vlc/VLCCrashHandler.java
index 44acbdd..764feaf 100644
--- a/vlc-android/src/org/videolan/vlc/VLCCrashHandler.java
+++ b/vlc-android/src/org/videolan/vlc/VLCCrashHandler.java
@@ -21,6 +21,7 @@
 package org.videolan.vlc;
 
 import java.io.BufferedWriter;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
@@ -79,18 +80,29 @@ public class VLCCrashHandler implements UncaughtExceptionHandler {
         CharSequence timestamp = DateFormat.format("yyyyMMdd_kkmmss", System.currentTimeMillis());
         String filename = name + "_" + timestamp + ".log";
 
+        FileOutputStream stream;
         try {
-            FileOutputStream stream = new FileOutputStream(filename);
-            OutputStreamWriter output = new OutputStreamWriter(stream);
-            BufferedWriter bw = new BufferedWriter(output);
+            stream = new FileOutputStream(filename);
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+            return;
+        }
+
+        OutputStreamWriter output = new OutputStreamWriter(stream);
+        BufferedWriter bw = new BufferedWriter(output);
 
+        try {
             bw.write(log);
             bw.newLine();
-
-            bw.close();
-            output.close();
         } catch (IOException e) {
             e.printStackTrace();
+        }finally {
+            try {
+                bw.close();
+                output.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
         }
     }
 



More information about the Android mailing list