[Android] AudioService: do not leak resources in exceptions

Jean-Baptiste Kempf git at videolan.org
Fri Jul 18 22:12:43 CEST 2014


vlc-ports/android | branch: master | Jean-Baptiste Kempf <jb at videolan.org> | Fri Jul 18 22:09:43 2014 +0200| [6cd6fa17a3b70a98691d7d4f234e7e1d03524ef9] | committer: Jean-Baptiste Kempf

AudioService: do not leak resources in exceptions

And catch all exceptions

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

 .../src/org/videolan/vlc/audio/AudioService.java   |   21 ++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
index 2c346b8..36ec05a 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
@@ -1382,8 +1382,8 @@ public class AudioService extends Service {
             return;
 
         String line;
-        FileInputStream input;
-        BufferedReader br;
+        FileInputStream input = null;
+        BufferedReader br = null;
         int rowCount = 0;
 
         int position = 0;
@@ -1396,7 +1396,7 @@ public class AudioService extends Service {
             br = new BufferedReader(new InputStreamReader(input));
             currentMedia = br.readLine();
             mShuffling = "1".equals(br.readLine());
-            br.close();
+            br.close(); br = null;
             input.close();
 
             // read MediaList
@@ -1408,16 +1408,21 @@ public class AudioService extends Service {
                     position = rowCount;
                 rowCount++;
             }
-            br.close();
-            input.close();
 
             // load playlist
             mInterface.load(mediaPathList, position, false);
-        } catch (IOException e) {
-            e.printStackTrace();
-        } catch (RemoteException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
+        finally {
+            try {
+                if (br!= null) br.close();
+                if (input != null) input.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+
     }
 
     private synchronized void saveCurrentMedia() {



More information about the Android mailing list